Queries That Select Records (Chapter 6 – 7)

Click here to load reader

download Queries That Select  Records (Chapter 6 – 7)

of 42

description

Queries That Select Records (Chapter 6 – 7). Query Basics. A Microsoft Access query is a question about the information stored in Access tables . - PowerPoint PPT Presentation

Transcript of Queries That Select Records (Chapter 6 – 7)

INTRODUCTION ACCESS 2010

Queries That Select Records(Chapter 6 7)Query BasicsA Microsoft Access query is a question about the information stored in Access tables.Your query can be a simple question about data in a single table, or it can be a more complex question about information stored in several tables. After run query, Microsoft Access returns only the information you requested.Creating Queries in Design ViewThe best starting point for query creation is the Design view. ChooseCreate Queries Query Design. Selectthe table that has the data you want, and then click Add Click Close.

Creating Queries in Design ViewSelect 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.

Creating Queries in Design ViewChoosea 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.

Getting the top recordsUse the Top Values box on the Design View toolbar to see the top records produced by the query. Open query in Design view.Sort table so that the records youre most interested in are at the top.In the Query Tools Design Query Setup In return box, choose a different option

Creating Queries by WizardCreate a simple Query by using wizard.The Query wizard works by asking you a series of questions.ChooseCreate Queries Query Wizard.

Creating Queries by WizardChoose a query type. The Query wizard includes a few common kinds of queries. Simple Query Wizard.Crosstab Query Wizard.Find Duplicates Query Wizard.Find Unmatched Query WizardOK.In the Tables/Queries box, choose the table that has the data you want.

Creating Queries by WizardAdd the fields you want to see in thequery resultsClick Next. Enter query name Click Finish.

Understanding the SQL ViewBehind the scenes, every query is actually a text command written in a specialized language called SQL (Structured Query Language).To take a look at the SQL command for a query, right-click the tab title, and then choose SQL view. Example: 1 SELECT Products.ID, Products.ProductName, Products.Price2 FROM Products3 WHERE (((Products.Price)>50))4 ORDER BY Products.Price;Understanding the SQL ViewEvery query has common ingredients (thnh phn), 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.Queries and Related TablesA join line connects tables in the query design. The join line connects the primary key in one table to the foreign key in another table.By default, an Access query returns only records where data exists on both sides of a relationship. To see records that are not matched on the other side of the join. You must modify the default query join. Queries and Related TablesCan create joins between tables in these three ways: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 behaviorQueries and Related Tables

Calculated FieldsTo create a calculated field, you need to supply two details:

Example: define the PriceWithTax calculated field: PriceWithTax: [Price] * 1.10

Field Name : ExpressionField NameExpressionSimple Math with Numeric FieldsSimple Math with Numeric FieldsOperator NameExampleResult+Addition1+12Subtraction110*Multiplication2*24^Exponentiation2^38/Division5/22.5\Integer division5\22ModModulus5 Mod 21Expressions with TextTo join text, use the ampersand (&) operator.For example:Create a FullName field from the FirstName and LastName fields:FullName: [FirstName] & & [LastName]To The price is to appear before each price value, use this calculated field:Price: "The price is: " & [Price]Query FunctionsA function is a built-in algorithm that takes some data that you supply, performs a calculation, 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. Using a FunctionTo 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)The Expression BuilderTo quickly find the functions you want, Access provides a tool called the Expression Builder.To launch the Expression Builder:Open aquery in Design view.Right-click the box where you want to insert your expression, and then choose Build.Add oredit the expression.Click OK.The Expression BuilderTo find a function:Expand the Functions item, and choose Built-In Functions. Next, choose a function category in the Expression Categories list. The Expression Values list will show all the functions in that category. Double click on functions name to insert it into your expression. Formatting Numbers

Formatting NumbersFormat(): mathematical function that transforms numbers into text. Example: SalePrice: [Price] * 0.95Use the Format() function to apply a currency format:SalePrice: Format([Price] / 0.95, "Currency")Formatting NumbersFormatDescriptionExampleCurrencyDisplays a number with two decimal places, thousand separators, and the currency sign.$1,433.20FixedDisplays a number with two decimal places.1433.20StandardDisplays a number with two decimal places and the thousands separator.1,433.20PercentDisplays a percent value. Displays 2 digits to the right of the decimal place.143320.00%ScientificDisplays a number in scientific notation, with two decimal places.1.43E+03Yes/NoDisplays No if the number is 0 and Yes if the number is anything else. YesFunctionsMore Mathematical Functions (Page 230)Text Functions (Page 232)Date Functions (Page 234)Summarizing DataTotal function is used to group the records to arrive at totals and subtotals. That way, you can review large quantities of information much more 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.Summarizing DataThese operations are used to summarize data:CountSumAverageMaxMin Summarizing DataCreate a totals query:Createa new query by choosing Create Queries Query Design.Add the tables youwantto use fromthe Show Table dialog box, and then click Close.Add the fields you want to use.Choose Query Tools Design Show/Hide Totals.Summarizing DataFor eachfield, choose an option from the Total box. This option determines whether the field is used in a calculation or used for grouping. Every field must belong to one of these categories:Its used in a summary calculation (like averaging, counting, ). Its used for grouping.Its used for filtering. Summarizing DataChoice in the Total boxDescriptionGroup BySubgroups records based on the values in this 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.CountCounts the number of records (no matter which field you use).FirstRetains the first value in this field.LastRetains the last value in this field.Summarizing DataExample: Calculate Total for every Order

Query ParametersQuery parameters let you create flexible queries by enter one pieces of information. Every time you run the query, Access prompts you to supply the missing values. These missing values are the query parameters.Usually, query parameters are used in filter conditions. When run the query, you fill in the value you want to use at that particular moment.Query ParametersTo 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.Crosstab QueriesA crosstab query is a powerful summary tool that examines huge amounts of data and uses it to calculate information like subtotals and averages.Crosstab queries use two key ingredients: grouping and summary functions. The grouping is used to organize the rows into small sets. The summary function is used to calculate a single piece of information for each group.Crosstab QueriesCreating Crosstab Queries: have two ways to create 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.Crosstab QueriesCreating a Crosstab Query with the WizardDisplay 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.Crosstab QueriesSelect the field(s) whose values you want to use as row headings click Next.Select the field(s) whose values you want to use as column headings click Next.If select a date field as the column headings, Choose how to group dates from the list Next.Choose the field whose values grouped by the row and column headings that are selected.Crosstab QueriesSelect a grouping method from the Functions list.Choose whether to include row sums by selecting the check box on the same page as the function choices, and then click Next.Name the query and then click Finish to see the Crosstab query.Crosstab QueriesCreating a Crosstab query in Design view: A simple Crosstab query has three fields:One used for row headingsOne used for column headings.The Value field, which contains the data that you want to appear in the cells of the table. Tell Access how to summarize your data in the Crosstab query by choosing from these choices: Sum, Avg, Min, Max, Count, StDev, Var, First, or Last.Crosstab QueriesChoose Create Queries Query Design.Add the table or query you want to use in crosstab query.Query Tools Design Query Type Crosstab.Choose the fields use for row labels and in the Crosstab row, choose the Row Heading.Choose the fields use for Column labels and in the Crosstab row, choose the Column Heading.Crosstab QueriesSelect the field containing the values that you want aggregated, in the Crosstab row, choose the Value option.Set the Total row:Group By option for column heading and row heading. Choose summarize the data for the Value field column.Crosstab QueriesExample: