Description of the SQL Query WizardLast reviewed: April 30, 1996Article ID: Q118449 |
The information in this article applies to:
SUMMARYThe information below describes the Structured Query Language (SQL) Query Wizard and how to use it to create an SQL query. This information is also found in FoxPro online Help in the "SQL Query Wizard" topic.
MORE INFORMATIONThe SQL Query Wizard searches one or more tables for records that match the criteria you specify. It creates a query file with a .QPR extension, the same type of query created when you use FoxPro's RQBE. FoxPro .QPR queries allow you to use aggregate functions (such as AVG, COUNT, MAX, MIN, or SUM) in your query. The query results are read-only; you can browse them or incorporate them into a report, but cannot update the records.
SQL QUERY WIZARD STEPSThe SQL Query Wizard has six main steps:
Step 1 of 6: Selecting TablesFirst, select the table or tables you want to use from the Available Tables list. NOTE: If you are using the Query Wizard from the Catalog Manager, the tables on which you want to base your query must reside in the open catalog. If there are no tables found in the catalog and you click the Query Wizard button, you will be prompted to name your query and select a table on which to base the query. Next, RQBE will start from which you can create a query. If you are not using the Wizard from the Catalog Manager, you can also choose the Open Table... button to use a table that is not currently open.
Step 1a of 6: Joining TablesWhen you select more than one table, the wizard displays this screen so you can join the tables. Select matching fields from the parent table and the child table, then choose Add. For example, if you are setting a join between the Customer table and the Invoices table, you could set Customer.cno equal to Invoices.cno. If you want to delete the relationship between two tables, select the relationship and choose the Remove button. When you are done joining tables, choose Next to continue to the next step.
Step 2 of 6: Selecting Fields and Adding ExpressionsSelect the fields you want to include in the query. The list of available fields includes fields from all the tables you have selected. To select fields, you can:
Adding Expressions to Your Query: You can also add expressions to your query. An expression is a combination of operators, functions, and field names that evaluate to a single value. For example, you might want your query to include the sum of all prices:
SUM(cost)or include a combination of two fields, such as:
Cost+TaxWhen you choose the Expression... button, the wizard displays the Expression dialog, where you can create the expression to add to your query. To Create an Expression with the Expression Dialog:
Step 3 of 6: Grouping the Query Results From the Available Fields list, select up to three fields by which to group the records in your query results. For example, if you choose to group by state and zip code, the records will be sorted into groups by state, then grouped and sorted by zip code within each state. This type of grouping can be useful in conjunction with any expressions you incorporate into your query. For example, you could include an expression which does a SUM of all sales, then group the results by State, to find the total sales by state.
Step 3a of 6: Setting Additional Grouping OptionsYou can add optional modifiers to your grouping criteria to control how records are grouped in the query results. When you group by entire field, each unique field is placed in a separate group. If you want to make the groupings larger, you can specify that the fields be grouped by their first character, or their first two or three characters. For example, if you were grouping the names Smith, Samuels, and Snyder by the entire field, each name would appear in its own group. If you grouped by first letter, all three names would appear in the same group. For numeric fields, by default each unique number constitutes a separate group. If you choose, for example, to group by 10s, then all the values from 0 to 9 would be part of the same group.
Step 4 of 6: Setting the Sort Order for the RecordsIn this screen, you determine how the records retrieved by the query will be sorted. Select up to three fields from the Available Fields list to set the sort order of the records. You can sort on fields from any of the selected tables. For example, if you choose Customer.Name and Invoice.Ino, the records will be sorted by customer name, from the Customer table, and then by invoice number from the Invoices table. Choose the Ascending option to sort records from the beginning of the alphabet, the lowest number, or the earliest date; choose the Descending option to sort records from the end of the alphabet, the highest number, or the latest date.
Step 5 of 6: Limiting the Contents of the QueryIn this screen you can add an optional expression to further limit the records retrieved by the query. The effect is like filtering the results so that only records meeting certain criteria are included in the query results. For example, you could limit the search to last names starting with J by using the expression:
Customer.lastname BeginsWith JFirst select a field from the Fields list and an operator from the Operator list. Then enter a comparison value in the Value field, and choose the Add button to add the new expression to the expression box. Entering Values: In the Value field, type the value you want the query to compare the field against. For example, if you are looking for customer records with a particular last name, type that name in the Value box, after choosing the last name field and the equal-sign operator. The resulting query expression would look like this:
LAST_NAME = JOHNSONCombining Expressions: You can create more complex queries by adding more expressions. When you combine expressions, they are combined with an AND operator by default. For example:
STATE = WA AND ORDER > $100If you want the query to find records that match either expression, but not both, choose the OR button to add an OR operator between expressions. For example:
STATE = WA OR STATE = CAYou can add parentheses around an expression to make sure the expressions are evaluated as a unit. For example:
(STATE=WA OR STATE=CA) AND ORDER > $100To Create an Expression:
Step 6 of 6: Previewing, saving, and browsing your queryChoose the Preview... button to see the results of your query in a Browse window. If you are satisfied with the results, choose one of the following options, and choose the Finish button.
|
Additional reference words: FoxDos FoxWin 2.60 foxhelp.hlp foxhelp.dbf
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |