Top Banner

Click here to load reader

of 42

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1. Queries That Select Records (Chapter 6 7)

2. Query Basics A Microsoft Access query is a question aboutthe information stored in Access tables. Your query can be a simple question about datain a single table, or it can be a more complexquestion about information stored in severaltables. After run query, Microsoft Access returns onlythe information you requested. 3. Creating Queries in Design View The best starting point for query creation is the Design view. ChooseCreate Queries Query Design. Select the table that has the data you want,and then click Add Click Close. 4. Creating Queries in Design View Select the fields you want to include in query. Arrange the fields from left to right in the order to appear in the query results. If you want to hide one or more columns, then clear the Show checkbox for those columns. 5. Creating Queries in Design View Choosea sort order. Set your filtering criteria by place the expression into the Criteria box for the appropriate field. Choose Query Tools Design Results Run. Save the query. 6. Getting the top records Use the Top Values box on the Design Viewtoolbar to see the top records produced by thequery. Open query in Design view. Sort table so that the records youre mostinterested in are at the top. In the Query Tools Design Query Setup In return box, choose a different option 7. Creating Queries by Wizard Create a simple Query by using wizard. The Query wizard works by asking you a seriesof questions. ChooseCreate Queries Query Wizard. 8. Creating Queries by Wizard Choose a query type. The Query wizardincludes a few common kinds of queries. Simple Query Wizard. Crosstab Query Wizard. Find Duplicates Query Wizard. Find Unmatched Query Wizard OK. In the Tables/Queries box, choose the tablethat has the data you want. 9. Creating Queries by Wizard Add the fields you want to see in the queryresults Click Next. Enter query name Click Finish. 10. Understanding the SQL View Behind the scenes, every query is actually a textcommand written in a specialized languagecalled SQL (Structured Query Language). To take a look at the SQL command for a query,right-click the tab title, and then choose SQLview. Example: 1 SELECT Products.ID, Products.ProductName, Products.Price 2 FROM Products 3 WHERE (((Products.Price)>50)) 4 ORDER BY Products.Price; 11. Understanding the SQL View Every query has common ingredients (thnhphn), represented by: SELECT: list of fields appear in the query results. FROM: indicates the table (or tables) that youre searching. WHERE: indicates the start of your filter conditions. ORDER BY: define the sorting order. 12. Queries and Related Tables A join line connects tables in the query design.The join line connects the primary key in onetable to the foreign key in another table. By default, an Access query returns onlyrecords where data exists on both sides of arelationship. To see records that are not matched on theother side of the join. You must modify thedefault query join. 13. Queries and Related Tables Can create joins between tables in these threeways: By creating relationships between the tables when you design the database. By selecting two tables for the query that have a field in common that has the same name By modifying the default join behavior 14. Queries and Related Tables 15. Calculated Fields To create a calculated field, you need to supplytwo details:Field Name : Expression Example: define the PriceWithTax calculatedfield: PriceWithTax: [Price] * 1.10FieldExpressio Namen 16. Simple Math with Numeric Fields Simple Math with Numeric FieldsOperator NameExampleResult +Addition1+12 Subtraction 110 *Multiplication2*24 ^Exponentiation2^38 /Division5/22.5 Integer division522Mod Modulus 5 Mod 21 17. Expressions with Text To join text, use the ampersand (&) operator. For example: Create a FullName field from the FirstNameand LastName fields: FullName: [FirstName] & & [LastName] To The price is to appear before each pricevalue, use this calculated field:Price: "The price is: " & [Price] 18. Query Functions A function is a built-in algorithm that takessome data that you supply, performs acalculation, and then returns a result. Functions is used in: Calculated fields. To add information to your query results. Filter conditions. To determine what records you see in a query. Visual Basic code. 19. Using a Function To use a function: Enter the function name, followed by parentheses. Inside the parentheses, put all the information the function needs in order to perform its calculations. Example: SalePrice: Round([Price] * 0.95, 2) 20. The Expression Builder To quickly find the functions you want, Accessprovides a tool called the Expression Builder. To launch the Expression Builder: Open a query in Design view. Right-click the box where you want to insert your expression, and then choose Build. Add or edit the expression. Click OK. 21. The Expression Builder To find a function: Expand the Functions item, and choose Built-In Functions. Next, choose a function category in theExpression Categories list. The Expression Values list will show all thefunctions in that category. Double click on functions name to insert itinto your expression. 22. Formatting Numbers 23. Formatting Numbers Format(): mathematicalfunction thattransforms numbers into text. Example: SalePrice: [Price] * 0.95 Use the Format() function to apply a currency format:SalePrice: Format([Price] / 0.95, "Currency") 24. Formatting Numbers Format Description Example Displays a number with two decimal places,Currency $1,433.20 thousand separators, and the currency sign.FixedDisplays a number with two decimal places. 1433.20 Displays a number with two decimal placesStandard and the thousands separator. 1,433.20 Displays a percent value. Displays 2 digits toPercent 143320.00% the right of the decimal place. Displays a number in scientific notation, withScientific two decimal places.1.43E+03 Displays No if the number is 0 and Yes if theYes/No number is anything else.Yes 25. Functions More Mathematical Functions (Page 230) Text Functions (Page 232) Date Functions (Page 234) 26. Summarizing Data Total function is used to group the records toarrive at totals and subtotals. That way, you canreview large quantities of information muchmore easily. Example: Counting all the students in each class. Counting the number of orders placed by each customer. Totaling the amount of money spent on a single product. 27. Summarizing Data These operations are used to summarize data: Count Sum Average Max Min 28. Summarizing Data Create a totals query: Create a new query by choosing CreateQueries Query Design. Add the tables you want to use from the ShowTable dialog box, and then click Close. Add the fields you want to use. Choose Query Tools Design Show/HideTotals. 29. Summarizing Data For eachfield, choose an option from the Totalbox. This option determines whether the field isused in a calculation or used for grouping. Every field must belong to one of thesecategories: Its used in a summary calculation (like averaging, counting, ). Its used for grouping. Its used for filtering. 30. Summarizing Data Choice in the DescriptionTotal box Subgroups records based on the values in thisGroup By field.SumAdds together the values in this field.AvgAverages the values in this field.MinRetains the smallest value in this field.MaxRetains the largest value in this field. Counts the number of records (no matterCount which field you use).FirstRetains the first value in this field.Last Retains the last value in this field. 31. Summarizing Data Example: Calculate Total for every Order 32. Query Parameters Query parameters let you create flexiblequeries by enter one pieces of information. Every time you run the query, Access promptsyou to supply the missing values. These missingvalues are the query parameters. Usually, query parameters are used in filterconditions. When run the query, you fill in the value youwant to use at that particular moment. 33. Query Parameters To create a query that uses parameters: Create a new query by choosing Create Queries Query Design. From the Show Table dialog box, add the tables you want to use. Choose Query Tools Design Show/Hide Parameters. The Query Parameters dialog box appears. Choose a name and data type for parameter. Click OK to close the Query Parameters dialog box. 34. Crosstab Queries A crosstab query is a powerful summary toolthat examines huge amounts of data and uses itto calculate information like subtotals andaverages. Crosstab queries use two key ingredients:grouping and summary functions. The grouping is used to organize the rows intosmall sets. The summary function is used to calculate asingle piece of information for each group. 35. Crosstab Queries Creating Crosstab Queries: have two ways tocreate a crosstab query: The Crosstab Query Wizard provides an automated way to create a Crosstab query. The wizard works only with one table or query. Build Crosstab Query by hand. 36. Crosstab Queries Creating a Crosstab Query with the Wizard Display the Create tab on the Ribbon. Click the Query Wizard button in the Macros & Code group on the Ribbon. The New Query dialog box opens. Select the Crosstab Query Wizard option and then click OK. Select the table or query that contains all the fields you need for your Crosstab query, and then click Next. 37. Crosstab Queries Select the field(s) whose values you want touse as row headings click Next. Select the field(s) whose values you want touse as column headings click Next. If select a date field as the column headings,Choose how to group dates from the listNext. Choose the field whose values grouped by therow and column headings that are selected. 38. Crosstab Queries Select a grouping method from the Functionslist. Choose whether to include row sums byselecting the check box on the same page asthe function choices, and then click Next. Name the query and then click Finish to seethe Crosstab query. 39. Crosstab Queries Creating a Crosstab query in Design view: Asimple Crosstab query has three fields: One used for row headings One used for column headings. The Value field, which contains the data thatyou want to appear in the cells of the table.Tell Access how to summarize your data in theCrosstab query by choosing from thesechoices: Sum, Avg, Min, Max, Count, StDev, Var,First, or Last. 40. Crosstab Queries Choose Create Queries Query Design. Add the table or query you want to use incrosstab query. Query Tools Design Query Type Crosstab. Choose the fields use for row labels and in theCrosstab row, choose the Row Heading. Choose the fields use for Column labels and inthe Crosstab row, choose the Column Heading. 41. Crosstab Queries Select the field containing the values that youwant aggregated, in the Crosstab row, choosethe Value option. Set the Total row: Group By option for column heading androw heading. Choose summarize the data for the Valuefield column. 42. Crosstab Queries Example: