Computer Science & Engineering 2111 Inner Joins and Advanced Queries CSE 2111 Lecture-Advanced Queries 1 Sorting Results in a Query Use the Sorting line - Ascending or Descending to sort the resulting Query Table To sort multiple fields – The computer will apply sort precedence for sorting fields listed on a query from left to right. Sorting can be combined with any of the filtering techniques (criteria) we have already discussed. 2 CSE 2111 Lecture-Advanced Queries 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.. Query Name: NameSort Tables Required: Client Foreign Keys: None Join Type: None Field LName FName Table Client Client Sort Ascending Ascending Show x x Major Sort Minor Sort Criteria OR OR CSE 2111 Lecture-Advanced Queries 3 The data table The resulting dynaset 4 CSE 2111 Lecture-Advanced Queries 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. Query Name: CityStateSort Tables Required: Client Foreign Keys: None Join Type: None Field City State Table Client Client Ascending Sort Show City x Ascending x Criteria OR OR Major Sort CSE 2111 Lecture-Advanced Queries Minor Sort 5 The data table The resulting dynaset 6 CSE 2111 Lecture-Advanced Queries Query Name: InnerJoinExample Tables Required: Client/Payments Foreign Keys: ClientID Join Type: Inner Field ClientID Table Client LastName Amount Client Payments Sort Show x x x Criteria OR OR CSE 2111 Lecture-Inner Joins in Queries 7 The data tables 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 Queries 8 Guidelines when using more than one table in a query • 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. CSE 2111 Lecture-Inner Joins in Queries 9 Write a query that lists the first name, last name, and payment method for each payment made by the client. Query Name: PaymentMethod Tables Required: Client/Payments/PaymentMethod Foreign Keys: ClientID/PaymentType Join Type: Inner Field First Name Last Name MethodType Table Total Sort Client Payments Show Criteria OR x Client x x CSE 2111 Lecture-Aggregate Fields in Queries 10 The data tables The resulting dynaset CSE 2111 Lecture-Aggregate Fields in Queries 11 What if we want to create a field in a query that is the result of a calculation? 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 . Query Name: DiscountCharges Tables Required: Client/Charges Foreign Keys: ClientID Join Type: Inner Field ClientID Amount Table Client Charges Discount: [Amount] * 0.10 New Charge: [Amount] – [Discount] Sort Show x x x x Criteria OR OR CSE 2111 Lecture-Calculated Fields in Queries 12 The resulting dynaset Notice new field names 13 CSE 2111 Lecture-Calculated Fields in Queries To Aggregate fields in a Query use the Group by feature Group by Use the Group by feature to summarize a field – calculating its average, min, max, sum etc. Select the group(s) you wish to aggregate Access will automatically group by the lowest detail level field. In the desired field(s) select the function (sum, average, etc) using the pull down menu Sum Avg Min Max Count StDev Var First Last Expression Where CSE 2111 Lecture-Aggregate Fields in Queries 14 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 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. Query Name: PaymentSummary Foreign Keys: ClientID First Name Last Name Field Table Total Sort Show Criteria OR OR Tables Required: Join Type: ClientID Amount Client Client Payments Payments Group by Group by Count Sum x x x CSE 2111 Lecture-Aggregate Fields in Queries Client/Payments Inner x 16 The data tables The resulting dynaset Notice new field names 17 CSE 2111 Lecture-Aggregate Fields in Queries 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. Query Name: ChargesSummary Foreign Keys: Field First Name ClientID Last Name Join Type: Amount ChargeDate Table Client Total Group by Group by Sum Client Tables Required: Charges Client/Charges Inner New Charge: [SumOfAmount] * 1.20 Charges Where Expression Sort Show Criteria x x x x > #5/1/2008# OR OR CSE 2111 Lecture-Aggregate Fields in Queries 18 The data tables Notice new field names The resulting dynaset 19 CSE 2111 Lecture-Aggregate Fields in Queries
© Copyright 2026 Paperzz