|
|
|
|
- SQL Sum query on the entire data file
- SQL sum query on a grouping
- SQL sum query on a grouping through programming
How to create an SQL query to get a sum?
To perform a sum of values coming from a data file, you have the ability to perform a browse loop and to add up a value in a Numeric variable. The best method to perform this sum consists in using an SQL query. Let's see how to perform a sum by using: SQL Sum query on the entire data file A Sum query is used to add up a value found in all the records of a data file. In this example, we are going to perform the sum of all orders found in the ORDER file. The steps for creating this SQL sum query are as follows: - Click
in the quick access buttons. - In the window that is displayed, click "Queries".
- The query creation wizard starts.
- Specify that you want to create a select query ("Select" option).
Go to the next step of the wizard. - The query description window appears.
- Give a name and a caption to the query:
- In the left section of the description window, choose the file item for which the sum will be performed (TotalBT in our example).
- Double-click the item name to add it into the list of query items:
- In the right section of the editor, click the "Sum" button. The following window appears:
- Validate. The query description window is updated:
- Validate the query description window. The query is displayed in the editor:
- Save the query (Ctrl + S).
- Press F2 to see the SQL code:
- Run the query test (GO in the quick access buttons).
- The query can be run in the program by HExecuteQuery.
SQL sum query on a grouping You have the ability to create a query that performs the sum of a value according to a group criterion. In this example, we are going to perfom the sum or ordes by delivery mode. The steps for creating a Sum query on a grouping are as follows: - Create a Sum query (see previous paragraph).
- In the data file, select the item that will be used as group argument. In our example, we are going to choose the caption of delivery mode.
- Add this item to the query items (double-click the item name for example).
- Validate the query description window. The query is displayed in the editor:
- Save the query (Ctrl + S).
- Press F2 to see the SQL code:
- Run the query test (GO in the quick access buttons).
- The query can be run in the program by HExecuteQuery.
SQL sum query on a grouping through programming The SQL queries can be directly written through programming in the WLanguage code. To do so, you must: - Create a variable of type Data source to represent the query at runtime.
- Create a Character String variable to contain the SQL code of the query and write the SQL code in this variable.
- Run the SQL query with HExecuteSQLQuery.
- Browse the result with the HReadXXX functions.
Code sample Src1 is Data Source sSQLCode is string  // Sum or orders by payment mode sSQLCode = [ SELECT PAYMENT.PaymentCap, SUM(ORDER.TotalBT) AS SumOrders FROM ORDER, PAYMENT WHERE ORDER.PaymentID=PAYMENT.PaymentID GROUP BY PaymentCap ]  HExecuteSQLQuery(Src1, hQueryDefault, sSQLCode) FOR EACH Src1 Trace(Src1.PaymentCap, Src1.SumOrders) END
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|