Difference between revisions of "EN/documentation/ooo3 user guides/getting started/creating queries and reports"

From Wiki
Jump to navigationJump to search
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=Documentation/OOo3_User_Guides/Getting Started/Entering data in a form
+
|PrevPage=EN/documentation/ooo3_user_guides/getting_started/entering_data_in_a_form
|NextPage=Documentation/OOo3_User_Guides/Getting Started/Creating reports
+
|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.

Documentation note.png Queries blur the differences between a database and a data source. A database is only one type of data source. However, searching for usable information from a data source requires a query. Since the query (one part of a database) does this, the data source appears to become one part of that database: its table or tables. Query results, themselves, are special tables within the database.

Top of page

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.

Documentation note.png When working with a query, more than one table can be used. Since different tables may contain the same field names, the format for naming fields in a query is Table name and field name. A period (.) is placed between the table name and the field name.
File:QueryWizard.png
First page of the Query Wizard.

Top of page

Step 1: Select the fields.

  1. 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.
  2. 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.
  3. 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.
  4. Click Next.
File:QueryFields.png
List of fields added to the query.
Tip.png 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.


Top of page

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.

File:BaseSortBy.png
Sorting order page.
  1. 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.
  2. Click the second Sort by dropdown list.
    • Click CD-Collection.ArtistTitle.
    • Select Ascending or Descending according to the order you want.
  3. Repeat this process for CD-Collection.DatePurchased.
  4. Click Next.

Top of page

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
Documentation note.png These conditions apply to numbers, letters (using the alphabetical order), and dates.
  1. Since we are only searching for one thing, we will use the default setting of Match all of the following.
  2. We are looking for a particular artist, so select is equal to.
  3. Enter the name of the artist in the Value box. Click Next.

Top of page

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.

Documentation note.png 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".

Top of page

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.

Documentation caution.png The procedures we will be using only work with relational databases. This is because of how relational databases are constructed. The elements of a relational database are unique. (The primary key insures this uniqueness.) That is, there are no two elements which are exactly alike. This allows us to select specific elements to place into our queries. Without the elements of the relational database being unique from all other elements, we could not perform these procedures.


Top of page

Step 1: Open the first query in Design View.

Click Create Query in Design View.

Step 2: Add tables.

File:BaseAddQT.png
Add Tables or Query window.
  1. Click Fuel to highlight it.
  2. Click Add.
  3. Click Close.
Tip.png Move the cursor over the bottom edge of the fuel table. The cursor become a single arrow with two heads. Drag the bottom of the table to make it longer and easier to see all of the fields in the table.


File:FuelTable.png
Fuel table in query.

Top of page

Step 3: Add fields to the table at the bottom.

  1. Double-click the FuelID field in the Fuel table.
  2. Double-click the Odometer field.
  3. Double-click the FuelQuantity field.

The table at the bottom of the query window should now have three columns.

Top of page

Step 4: Set the criterion for the query.

We want to the query's FuelID to begin with the numeral 1.

  1. Type >0 in the Criterion cell under FuelID in the query table
  2. Click the Run Query icon in the Query Design toolbar. This icon is circled in red below.
File:QueryDesignToolbar.png
Query Design toolbar.

The figures below contain the Fuel table with my entries and the query results based upon the Fuel table.

File:QueryTable.png
Query of 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.

Top of page

Step 6: Create the query to calculate the fuel economy.

  1. Click Create Query in Design View to open a new query.
  2. Add the Fuel table to the query just as you did in step 2: Add tables. But, do not close the Add Tables window.
  3. Add the End-Reading query to this query.
    1. Click the Query radio button to get the list of queries in the database.
    2. File:SelectQuery.png
      Selecting queries to add to another query.
    3. Click End-Reading.
    4. Click Add, and then click Close.

Top of page

Step 7: Add fields to the table at the bottom of the query.

File:TablesInQuery.png
Tables in this 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.

  1. Double-click FuelQuantity in the End-Reading query.
  2. Double-click Odometer in the End-Reading query.
  3. Double-click Odometer in the Fuel table.
File:AddFieldsToQ.png
Added fields to the query.

Top of page

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).

  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.
  2. File:BaseCalcFields.png
    Typing in calculation of fields.
    Documentation caution.png When entering fields for these calculations, you must follow this format: table or query name followed by a period follow by the field name. For hyphenated or multiple-word names (table or query), use double quotes around the table or query name. The query will then add the rest of the double quotes as in the figure above.

    Use the arithmetical symbol between the two. More than one calculation can be done by using parentheses to group the arithmetical operations.


  3. Calculate the distance traveled:
    • Type "End-Reading".Odometer - Fuel.Odometer in the Field cell.
    • Type >0 in the Criterion cell.
    • File:DistTravel.png
      Field for distance traveled calculations.
  4. Calculate fuel economy:
    Type ("End-Reading".Odometer - Fuel.Odometer)/"End-Reading".FuelQuantity in the Field in the next column to the right.
  5. File:FuelEcon.png
    Fuel economy calculation field.

Top of page

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.

  1. 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.
  2. File:Query1.png
    Result of running the fuel economy query.
  3. Add Aliases: Type in aliases as they are listed below.
  4. File:QTAlias.png
    Query table with aliases added.
  5. Run the query again. The results are below.
  6. File:Qrunwithalias.png
    Query run with aliases.

    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.

  7. Hide a field that does not need to be seen.
    Remove the check in the box of the Visible cell.
  8. File:MakeFieldInvis.png
    Making a field invisible in a query run.
  9. Rerun the query.
  10. File:QwithAliases.png
    Query run with aliases.

Top of page

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.

Documentation note.png 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/

Top of page


Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).