Difference between revisions of "EN/documentation/ooo3 user guides/getting started/creating queries and reports"
m (1 revision) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | {{DISPLAYTITLE:Creating queries}} | + | <section begin=title /><noinclude>{{DISPLAYTITLE:</noinclude>Creating queries<noinclude>}}</noinclude><section end=title /> |
+ | |||
{{Documentation/GS3BaseTOC | {{Documentation/GS3BaseTOC | ||
|ShowPrevNext=block | |ShowPrevNext=block | ||
− | |PrevPage= | + | |PrevPage=EN/documentation/ooo3_user_guides/getting_started/entering_data_in_a_form |
− | |NextPage= | + | |NextPage=EN/documentation/ooo3_user_guides/getting_started/creating_reports |
}}__NOTOC__ | }}__NOTOC__ | ||
Queries are used to get specific information from a database. Using our CD-Collection table, we will create a list of albums by a particular artist. We will do this using the Wizard. The information we might want from the Fuel table includes what our fuel economy is. We will do this using the Design View. | Queries are used to get specific information from a database. Using our CD-Collection table, we will create a list of albums by a particular artist. We will do this using the Wizard. The information we might want from the Fuel table includes what our fuel economy is. We will do this using the Design View. |
Latest revision as of 09:59, 11 April 2012
Queries are used to get specific information from a database. Using our CD-Collection table, we will create a list of albums by a particular artist. We will do this using the Wizard. The information we might want from the Fuel table includes what our fuel economy is. We will do this using the Design View.
Using the Wizard to create a query
Queries created by the wizard provide a list or lists of information based upon what one wants to know. It is possible to obtain a single answer or multiple answers, depending upon the circumstances. Queries which require calculations are best created with the Design view.
In the main database window, click the Queries icon in the Databases section, then in the Tasks section, click Use Wizard to Create Query. The Query Wizard window opens. The information we want is what albums are by a certain musical group or individual (the album's author). We can include when each album was bought.
Step 1: Select the fields.
- Select the CD-Collection table from the dropdown list of tables.
- If the Tables selection is not Table: CD-Collection, click the arrow (circled in red in figure above).
- Click Table: CD-Collection in the list to select it.
- Select fields from the CD-Collection table in the Available fields list.
- Click AlbumTitle, and use the > button to move it to the Fields in Query list.
- Move the Artist and DatePurchased fields in the same manner.
- Use the up arrow to change the order of the fields: artist, album, and date purchased.
- Click the CD-Collection.Artist field.
- Click the up arrow to move it above CD-Collection.AlbumTitle.
- Click Next.
To change the order of the fields, select the field you want to move and click the up or down arrow to move it up or down. |
Step 2: Select the sorting order.
Up to four fields can be used to sort the information of our query. A little simple logic helps at this point. Which field is most important?
In our query, the artist is most important. The album title is less important, and the date purchased is of least importance. Of course, if we were interested in what music we bought on a given day, the date purchased would be the most important.
- Click the first Sort by dropdown list.
- Click CD-Collection.Artist to select it.
- If you want the artists to be listed in alphabetical order (a-z), select Ascending on the right. If you want the artist listed in reverse order (z-a), select Descending on the right.
- Click the second Sort by dropdown list.
- Click CD-Collection.ArtistTitle.
- Select Ascending or Descending according to the order you want.
- Repeat this process for CD-Collection.DatePurchased.
- Click Next.
Step 3: Select the search conditions.
The search conditions available are listed below. They allow us to compare the name we entered with the names of the artist in our database and decide whether to include a particular artist in our query or not.
- is equal to: the same as
- is not equal to: not the same as
- is smaller than: comes before
- is greater than: comes after
- is equal or less than: the same as or comes before
- is equal or greater than: the same as or comes after
- like: similar to in some way
- Since we are only searching for one thing, we will use the default setting of Match all of the following.
- We are looking for a particular artist, so select is equal to.
- Enter the name of the artist in the Value box. Click Next.
Step 4: Select type of query.
We want simple information, so the default setting: Detailed query is what we want. Click Next at the bottom of the window.
Since we have a simple query, the Grouping and Grouping conditions are not needed. Those two steps are skipped in our query. |
Step 5: Assign aliases if desired.
We want the default settings. Click Next.
Step 6: Overview.
Name the query (suggestion: Query_Artists). To the right of this are two choices. Select Display Query. Click Finish.
Step 7: Modify the query.
We are skipping this step since we have nothing to modify. If you select the Modify Query choice, the query would open in Design view. To make modifications, follow the instructions in the next section, “Using the Design View to create a query".
Using the Design View to create a query
Creating a query using Design View is not as hard as it may first seem. It may take multiple steps, but each step is fairly simple.
What fuel economy is our vehicle getting (miles per gallon in the USA)? This question requires creating two queries, with the first query being used as part of the second query.
Step 1: Open the first query in Design View.
Click Create Query in Design View.
Step 2: Add tables.
- Click Fuel to highlight it.
- Click Add.
- Click Close.
Step 3: Add fields to the table at the bottom.
- Double-click the FuelID field in the Fuel table.
- Double-click the Odometer field.
- Double-click the FuelQuantity field.
The table at the bottom of the query window should now have three columns.
Step 4: Set the criterion for the query.
We want to the query's FuelID to begin with the numeral 1.
- Type >0 in the Criterion cell under FuelID in the query table
- Click the Run Query icon in the Query Design toolbar. This icon is circled in red below.
The figures below contain the Fuel table with my entries and the query results based upon the Fuel table.
Step 5: Save and close the query.
Since this query contains the ending odometer reading for our calculations, name it End-Reading when saving it. Then close the query.
Step 6: Create the query to calculate the fuel economy.
- Click Create Query in Design View to open a new query.
- Add the Fuel table to the query just as you did in step 2: Add tables. But, do not close the Add Tables window.
- Add the End-Reading query to this query.
- Click the Query radio button to get the list of queries in the database.
- Click End-Reading.
- Click Add, and then click Close.
Step 7: Add fields to the table at the bottom of the query.
We are going to calculate the fuel economy. To do this we need the FuelQuantity and distance traveled. Since the FuelQuantity we want to use is at the ending odometer reading, we will use the End-Reading query to get it. We will also use the Odometer field from the Fuel table and End-Reading query.
- Double-click FuelQuantity in the End-Reading query.
- Double-click Odometer in the End-Reading query.
- Double-click Odometer in the Fuel table.
Step 8: Enter the FuelID difference field.
We want the difference between the FuelID value of the Fuel table and FuelID value of the End-Reading query to equal one (1).
- Type "End-Reading".FuelID - Fuel.FuelID in the field to the right of the Odometer field of the Fuel Table.
Type the numeral 1 (one) in the Criterion cell of this column. - Calculate the distance traveled:
- Type "End-Reading".Odometer - Fuel.Odometer in the Field cell.
- Type >0 in the Criterion cell.
- Calculate fuel economy:
Type ("End-Reading".Odometer - Fuel.Odometer)/"End-Reading".FuelQuantity in the Field in the next column to the right.
Step 9: Run the query and make some modification.
After we run the query to make sure it works correctly, we will hide all of the fields that we do not need.
- Click the Run Query icon in the Design Query toolbar. The results are below.
Notice that not all of the last column label is visible because some of the labels are long. We can fix this problem by using an alias for many of the fields. The labels are replaced by their aliases. - Add Aliases: Type in aliases as they are listed below.
- Run the query again. The results are below.
- Hide a field that does not need to be seen.
Remove the check in the box of the Visible cell. - Rerun the query.
We really do not need the column showing the difference between the FuelID fields from the table and query. So, we will hide it. While it will not be visible, it will still be used in the calculations.
Step 10: Close, save, and name the query.
My suggestion for a name is Fuel Economy.
There are obviously other calculations that can be made in this query such as cost per distance traveled and how much of the cost belongs to each of the payments types.
To fully use queries requires a knowledge of mathematics and specifically set operations (unions, intersections, and, or, complements, and any combinations of these). For example, we listed all of our criteria in one row. That means that all of these criteria have to be met before a row of values will be created in the query. This is how the and operator works on sets.
It also requires having a copy of the Hsqldb User Guide available from http://hsqldb.org/ |
Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY). |