130 likes | 374 Views
Tutorial 8. Advanced Queries. Notes. Switch to new database! Tutorial.08 folder Only Session 8.1 and 8.2. Create a crosstab query.
E N D
Tutorial 8 Advanced Queries
Notes • Switch to new database! • Tutorial.08 folder • Only Session 8.1 and 8.2
Create a crosstab query • A crosstab query performs aggregate function (arithmetic operations) calculations on the values of one database field and displays the results in a spreadsheet format. • Possible functions include • average, count, first field value, last field value, etc. • Crosstab Query Wizard will walk you through creating a crosstab query.
A select query versus a crosstab query This figure shows a Select query and a crosstab query. These two queries are based on the same data. In this case the crosstab query is using the Sum function to summarize the potential income for each country`. However, notice that the crosstab query data is much more summarized than the select query data.
Crosstab query aggregate functions The figure below shows the aggregate functions that can be used in a crosstab query.
The Crosstab Query Wizard steps • Select the table or query upon which you want to create the crosstab query. • Select fields to use as row headings. • Select fields to use as column headings. • Select the field that will be calculated upon and what calculation is to be performed.
The final Crosstab Query Wizard dialog box This figure shows the final Crosstab Query Wizard dialog box where you select the field to be used for the calculation, and which type of calculation to perform.
Other types of queries • Find duplicates • a select query that locates duplicate records in a table or query • Find unmatched • returns a datasheet for all records that do not have a matching record in the related table • Top values • Returns the top number top percentage of records • E.g. top 10 or top 25%
Action queries • Queries that make changes to the data in the underlying table(s). • Action queries can: • Create a new table (make-table query) • Add records to a table (append query) • Delete records from a table (delete query) • Update the data in a table (update query)
Action query considerations • These queries change the underlying tables permanently! Be very careful!!! • Always create a select query to test it out first • You can then view the results and determine if these are the records you want to alter. • When you are sure the query is right, run it as an action query.
Many-to-many Relationships Between Tables • A many-to-many relationship exists between tables when the tables involved have multiple matches in each of the tables. • For example, if you have a table containing student data and another table containing course data, you could say that this is a (M:N) relationship because a student can take many courses and a course can have many students • Many-to-many relationships require a third table that will “link” the two tables together in a one-to-many relationship.
A many-to-many relationship In the figure below, you see an illustration of a many-to-many relationship. Note that the Job table is used to join the Position table and the Student table. The Job table contains a PositionID and a StudentID for each position that a students holds. In this case, a student can have more than one position and a position can be held by more than one student.
A query datasheet for a many-to-many relationship Once you have created the third table (the table that joins the two tables in a many-to-many relationship) you can create a query based on all three tables. The following figure show a query datasheet based on the three tables.