ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / How to proceed? / Query editor
  • Overview
  • SQL query with external join to a data file (query editor)
  • SQL query with external join through programming
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
In SQL language, an external join is used to exclude or include file records with or without correspondences in the linked files.
By default, when creating a query between 2 linked data files, only the records with a correspondence between the 2 files are processed. To process the records without correspondence, an external join must be added.
Let's see how to perform an external join by using:
SQL query with external join to a data file (query editor)
This query is used to select the records found in a data file (main file) with a correspondence (join) to another data file (fsecondary file or linked file), knowing that all the records found in the main file do not necessarily have a correspondence in the secondary file.
In this example, we are going to select the records found in CUSTOMER file (main file) with the sum of orders in ORDER file (secondary file) for each customer. Some customers may have no order. Therefore, an external join must be added into the query to process the customer without orders.
The different steps for creating this SQL query with external join are as follows:
  1. Click New in the quick access buttons.
    • In the window that is displayed, click "Queries".
    • The query creation wizard starts.
  2. Specify that you want to create a select query ("Select" option).
    Query creation wizard

    Go to the next step of the wizard.
  3. The query description window appears.
  4. Give a name and a caption to the query:
    Name and caption of the query
  5. In the left section of the description window, choose the file items that will be used (in our example, the CUSTOMER file and the CustomerName, Company and City items then the ORDER file and the TOTALBT item).
  6. Double-click the names of the items to add them to the list of query items:
    Selection of items
  7. To perform the sum of orders, click the "Sum" button in the right section of the editor. The following window is displayed in the editor:
    Sum
  8. Validate. The query description window is updated:
    Query description
  9. Validate the query description window. The following query is displayed in the editor:
    Displaying the query in the editor
  10. To add the external join, double-click the "Join" area.
    join
  11. The description window of the join is displayed.
  12. To select the direction for the external join, check the "include..." box corresponding to the requested direction. In our example, the records found in CUSTOMER file without orders are included.
    Definition of joins
  13. Validate. The query description window is updated.
    Query description
  14. Save the query (Ctrl + S).
  15. Press F2 to see the SQL code:
    SQL code of query
  16. Run the query test (GO in the quick access buttons).
  17. The query can be run in the program by HExecuteQuery.
SQL query with external join through programming
The SQL queries can be directly written through programming in the WLanguage code. To do so, you must:
  1. Create a variable of type Data source to represent the query at runtime.
  2. Create a Character String variable to contain the SQL code of the query and write the SQL code in this variable.
  3. Run the SQL query with HExecuteSQLQuery.
  4. Browse the result with the HReadXXX functions.
Code example
Src1 is Data Source
sSQLCode is string
 
// Select the products whose price is greater than ...
sSQLCode = [
SELECT
CUSTOMER.CustomerName AS CustomerName,
CUSTOMER.Company AS Company,
CUSTOMER.City AS City,
SUM(ORDERS.TotalBT) AS the_sum_TotalBT
FROM
CUSTOMER
LEFT OUTER JOIN
ORDERS
ON CUSTOMER.CustNum = ORDERS.CustNum
GROUP BY
CUSTOMER.CustomerName,
CUSTOMER.Company,
CUSTOMER.City
]
 
HExecuteSQLQuery(Src1, hQueryDefault, sSQLCode)
FOR EACH Src1
Trace(Src1.CustomerName, Src1.Company, Src1.the_sum_TotalBT)
 
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help