Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.

Similar presentations


Presentation on theme: "Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries."— Presentation transcript:

1 Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries

2 Sorting Results in a Query Use the Sorting line - Ascending or Descending to sort the resulting Query Table l To sort multiple fields – The computer will apply sort precedence for sorting fields listed on a query from left to right. l Sorting can be combined with any of the filtering techniques (criteria) we have already discussed. 2 CSE 2111 Lecture-Advanced Queries

3 Write a query to list the Last name and First name of all clients. The list should be sorted in ascending order by Last name, and then by First name.. Field LNameFName Table Client Sort Ascending Show xx Criteria OR 3 Query Name:NameSortTables Required:Client Foreign Keys:NoneJoin Type:None Major Sort CSE 2111 Lecture-Advanced Queries Minor Sort

4 The data table The resulting dynaset 4 CSE 2111 Lecture-Advanced Queries

5 Write a query to list the city and state of all clients The list should be sorted in ascending order by State and then by City. Field CityStateCity Table Client Sort Ascending Show xx Criteria OR 5 Query Name:CityStateSortTables Required:Client Foreign Keys:NoneJoin Type:None Major Sort CSE 2111 Lecture-Advanced Queries Minor Sort

6 The data table The resulting dynaset 6 CSE 2111 Lecture-Advanced Queries

7 Field ClientIDLastNameAmount Table Client Payments Sort Show xxx Criteria OR 7 Query Name:InnerJoinExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner CSE 2111 Lecture-Inner Joins in Queries

8 The resulting dynaset Dynaset only includes records that have matching keys on both tables. This is called an inner join which is the default join type in Access CSE 2111 Lecture-Inner Joins in Queries8 The data tables

9 CSE 2111 Lecture-Inner Joins in Queries9 Only show the tables on the grid that will be used in your query. Adding extra tables to the QBE grid may cause the dynaset to display incorrectly. All tables shown in the query must either be directly or indirectly linked in order for the query to run correctly. Usually this is the case, but there are some scenarios where you will have to create the relationship on the QBE grid yourself. Guidelines when using more than one table in a query

10 Write a query that lists the first name, last name, and payment method for each payment made by the client. 10 Query Name:PaymentMethodTables Required:Client/Payments/PaymentMethod Foreign Keys:ClientID/PaymentTypeJoin Type:Inner Field First NameLast NameMethodType Table Client Payments Total Sort Show xxx Criteria OR CSE 2111 Lecture-Aggregate Fields in Queries

11 11 CSE 2111 Lecture-Aggregate Fields in Queries The resulting dynaset The data tables

12 You have decided to give all clients a 10% break on each charge they have made. Write a query to list the ClientID, each charge amount before the discount, the discount, and the charge amount after the discount. FieldClientIDAmountDiscount: [Amount] * 0.10New Charge: [Amount] – [Discount] TableClientCharges Sort Showxxxx Criteria OR 12 Query Name:DiscountChargesTables Required:Client/Charges Foreign Keys:ClientIDJoin Type:Inner What if we want to create a field in a query that is the result of a calculation? CSE 2111 Lecture-Calculated Fields in Queries

13 The resulting dynaset Notice new field names 13 CSE 2111 Lecture-Calculated Fields in Queries

14 To Aggregate fields in a Query use the Group by feature Use the Group by feature to summarize a field – calculating its average, min, max, sum etc. l Select the group(s) you wish to aggregate - Access will automatically group by the lowest detail level field. l In the desired field(s) select the function (sum, average, etc) using the pull down menu Group by Sum Avg Min Max Count StDev Var First Last Expression Where 14 CSE 2111 Lecture-Aggregate Fields in Queries

15 Query design grid's Total Row options: Group By: Define the groups you want to perform the calculations for. Aggregate Functions: Sum, Avg, Min, Max etc. Where: Specify criteria for a field, so only those records that meet this criteria will be included in the aggregates. Expression: Use if a calculated field is required in the query. Expressions will be calculated AFTER the fields are aggregated. 15 CSE 2111 Lecture-Aggregate Fields in Queries

16 Write a query that lists how many payments were made by each Client and their total payments. List the First Name, Last Name, the number of payments made, and the total dollar amount of these payments. 16 Query Name:PaymentSummaryTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner Field First NameLast NameClientIDAmount Table Client Payments Total Group by CountSum Sort Show xxxx Criteria OR CSE 2111 Lecture-Aggregate Fields in Queries

17 Notice new field names 17 CSE 2111 Lecture-Aggregate Fields in Queries The resulting dynaset The data tables

18 Write a query to summarize by Client, all of the charges made after 5/1/2008. Add a surcharge of 20% to these charges. List the First Name, Last Name, Original Charge and New Charge. 18 Query Name:ChargesSummaryTables Required:Client/Charges Foreign Keys:ClientIDJoin Type:Inner Field First Name Last Name AmountChargeDate New Charge: [SumOfAmount] * 1.20 Table Client Charges Total Group by SumWhereExpression Sort Show xxxx Criteria > #5/1/2008# OR CSE 2111 Lecture-Aggregate Fields in Queries

19 Notice new field names 19 CSE 2111 Lecture-Aggregate Fields in Queries The resulting dynaset The data tables


Download ppt "Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries."

Similar presentations


Ads by Google