Query Summarizing Over a Calculated Field

From Apache OpenOffice Wiki
Jump to: navigation, search




Query Summarizing Over a Calculated Field

As a final example of a query, we will calculate the total sales of each person for each month and year. We will need to use the Date column from the Sales table twice, one to calculate the year and again to calculate the month, and we will also use the fields FirstName, LastName from the People table and Amount from the Sales table.

Step 1: Start the query in Design View

From the main Base window, click Queries in the left pane and then Create Query in Design View from the Tasks pane.

Step 2: Add tables to the query

In the Add Query or Table dialog, click on People and then the Add button and do the same for the Sales table. Click  Close .

Step 3: Add fields to the query

Double-click on the Date field from the Sales table twice, then double-click on FirstName and LastName from the People table and Amount from the Sales table to add them to the query definition table.

Step 4: Set the calculation of the year and the month

Change the Field row of the first Date column to read YEAR("Date") and change the Field row of the second Date column to read MONTH("Date"). Set the Alias row to read Sale_Year and Sale_Month for these two columns. The YEAR and MONTH functions are among the many functions provided by the HSQL database engine. A complete list is available in the documentation at https://www.hsqldb.org/doc/1.8/guide/guide.html#N1251E.

Step 5: Set the grouping and the sum function

Set the Function row of every field except Amount to read Group. Set the Function row of the Amount column to read Sum and set its alias to Sales_Sum.

Step 6: Set the sorting

For a more logical presentation of the data, set the Sort column of the year, month and LastName columns to read ascending.

Be sure to save the query and the main Base document. We saved the query with the name GrpCalcColumn, and we will use it in the reports in the next section.

The final configuration of the query definition table and the query results are shown in Figure 198.

AOO41GS8 039.png
Figure 198: Definition and result of a query that groups on a calculated column</center
Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools