The SQL command may be used to report from any supported data source or set of data sources. Standard TABLE phrases for formatting reports can be appended to the SQL statements to take advantage of a wide range of report preparation options.
Note: If you need to join data sources for your request, you have two options: use the JOIN command before you issue any SQL statements, or use the WHERE predicate in the SQL SELECT statement to join the required files dynamically. See SQL Joins.
SQL sql statement; [ECHO|FILE] [TABLE phrases] END
where:
Is the SQL command identifier, which invokes the SQL Translator.
Note: The SQL command components must appear in the order represented above.
Is a supported SQL statement. The statement must be terminated by a semicolon (;). It can continue for more than one line. See Supported SQL Statements.
Within the SQL statement, field names are limited to 48 characters (an ANSI standard Level 2 limitation). View names generated through the SQL CREATE VIEW statement are limited to 18 characters and subqueries can be nested up to 15 levels deep. Correlated subqueries are not supported by FOCUS and other non-relational data sources.
Are optional debugging phrases that capture the generated TABLE request. These options are placed after the SQL statement.
Writes the translated TABLE phrases to the named procedure. If you do not supply a file name, a default name is assigned when the request runs. The file is then deleted.
Are optional TABLE formatting phrases. See TABLE Formatting Phrases in SQL Requests.
Is required to terminate the procedure.
The following request contains an SQL statement and TABLE formatting commands:
SQL SELECT BODYTYPE, AVG(MPG), SUM(SALES) FROM CAR WHERE RETAIL_COST > 5000 GROUP BY BODYTYPE; TABLE HEADING CENTER "AVERAGE MPG AND TOTAL SALES PER BODYTYPE" END
You can include TABLE formatting phrases in an SQL request, subject to the following rules:
For details on headings and footings, see Using Headings, Footings, Titles, and Labels.
For details on ON TABLE HOLD or ON TABLE PCHOLD, see Saving and Reusing Your Report Output.
In this section: |
The SQL SELECT statement translates into one or more TABLE PRINT or TABLE SUM commands, depending on whether individual field display or aggregation is applied in the request. See Displaying Report Data.
The SQL statement SELECT * translates to a PRINT of every field in the Master File, and uses all of the fields of the Cartesian product. This is a quick way to display a file, provided it fits in a reasonable number of screens for display, or provided you use ON TABLE HOLD or ON TABLE PCHOLD to retain retrieved data in a file for reuse. See Saving and Reusing Your Report Output.
SQL functions (such as COUNT, SUM, MAX, MIN, AVG) are supported in SELECT lists and HAVING conditions. Expressions may be used as function arguments.
The function COUNT (*) translates to a count of the number of records produced by printing all fields in the Master File. This is the same as counting all rows in the Cartesian product that results from a SELECT on all fields.
Whenever possible, expressions in the SQL WHERE predicate are translated into corresponding WHERE criteria in the TABLE request. Expressions in SELECT lists generate virtual fields. The SQL HAVING clauses also translate into corresponding WHERE TOTAL criteria in the TABLE request. The SQL LIKE operator is translated directly into the corresponding LIKE operator in the WHERE criteria of the TABLE request. For details on record selection in TABLE requests, see Selecting Records for Your Report.
Only subqueries based on equality, when the WHERE expression is compared to a subquery by using an equal (=) sign, are supported. For example: WHERE field = (SELECT ...).
The SQL UNION operator translates to a TABLE request that creates a HOLD file for each data source specified, followed by a MATCH command with option HOLD OLD-OR-NEW, which combines records from both the first (old) data source and the second (new) data source. See Merging Data Sources.
For related information, see Supported SQL Statements and Use SQL Translator Commands.
SELECT without a FROM clause is supported for returning a one-row answer set consisting of one or more constant values. No Master File is needed for issuing this type of SELECT. One use for this syntax is to test functions. For example, the following SQL SELECT returns a literal value and the results of two function calls:
SQL SELECT 'MOD' AS FUNCTION, MOD(6,3) AS MOD1, MOD(5,3) AS MOD2; TABLE ON TABLE SET PAGE NOLEAD ON TABLE SET STYLE * GRID=OFF,$ ENDSTYLE END
The output is shown in the following image.
How to: |
Reference: |
When performing SQL joins, the formats of the joined fields must be the same. Join fields need not be indexed, and non-equijoins are supported.
Recursive, outer, and inner joins are supported. Inner join is the default.
Two syntax variations are supported for inner joins.
Variation 1
SQL SELECT fieldlist FROM file1 [alias1], file2 [alias2] [WHERE where_condition]; END
Variation 2
SQL SELECT fieldlist FROM file1 [alias1] INNER JOIN file2 [alias2] ON join_condition [INNER JOIN ...] [WHERE where_condition]; END
where:
Identifies which fields are retrieved from which data sources.
Joined fields in the SQL WHERE predicate must be qualified if the names are not unique. Specify them with their corresponding file names or file aliases. For example:
{file1|alias1}.field1, {file2|alias2}.field2
Introduces the data sources to be joined.
Are the data sources to be joined.
Are optional alternate names for the data sources to be joined.
Is an optional selection condition for the joined answer set. Joined rows that do not satisfy this condition are eliminated from the returned answer set. If omitted in Variation 1, the answer set is the Cartesian product of the two data sources.
Is the join condition.
SQL SELECT fieldlist FROM file1 {LEFT|RIGHT|FULL} JOIN file2 ON join_condition [{LEFT|RIGHT|FULL} JOIN ...] WHERE where_condition END
where:
Identifies which fields are to be retrieved from which data sources.
Joined fields in the SQL WHERE predicate must be qualified if the names are not unique. Specify them with their corresponding file names or file aliases. For example:
{file1|alias1}.field1, {file2|alias2}.field2
Introduces the data sources to be joined.
Are the data sources to be joined.
Are optional alternate names for the data sources to be joined.
Is the join condition. The condition must specify equality. For example, T1.A=T2.B.
Is an optional selection condition for the joined answer set. Joined rows that do not satisfy this condition are eliminated from the returned answer set.
Joins issued by the SQL Translator are assigned names in the format:
SQLJNMnn
where:
Is the SQL Translator join prefix.
Is a number between 01 and 16 assigned in the order in which the joins are created (FOCUS supports a maximum of 16 joins). The first join has the AS name SQLJNM01, the second join is named SQLJNM02, and so on, up to SQLJNM16.
All joins are automatically created and cleared by the SQL Translator. No user-specified joins are affected.
In the following statement, T.A and U.B are qualified field names:
SQL SELECT T.A, T.B FROM T, U WHERE T.A = U.B; END
In the following statement, A and B are aliases for the same data source, CAR. The output from CAR is pairs of B values that have the same A values:
SQL SELECT A.SEATS, B.SEATS FROM CAR A, CAR B WHERE A.MODEL = B.MODEL; END
Note that all field names in the SELECT clause must be unique or qualified.
In the following statement, B, C, and D are aliases for different data sources:
SQL SELECT B.FIELD1 AS B_FIELD1, B.FIELD2 AS B_FIELD2, D.FIELD1 AS D_FIELD1, D.FIELD2 AS D_FIELD2 FROM ((FILE1 B FULL OUTER JOIN FILE2 C ON B.FIELD2 = C.FIELD2 ) FULL OUTER JOIN FILE3 D ON C.FIELD2 = D.FIELD2 ) WHERE B.FIELD1 < 2 END
Multiple FULL OUTER JOINS are supported. However, they generate from a few to many temporary HOLD files.
For related information about index optimization and optimized join statements, see your Server documentation.
Reference: |
SQL Translator supports the commands CREATE TABLE and INSERT INTO table:
These commands enable you to create tables to enhance reporting efficiency.
Note: When applications are enabled, the Master File and data source are written to the APPHOLD directory. When applications are disabled, the Master File and data source are written to the TEMP directory.
The following shows a single-record insert, creating the table U with one record:
-* Single-record insert example. -* SQL CREATE TABLE U (A INT, B CHAR(6), C CHAR(6), X INT, Y INT); END SQL INSERT INTO U (A,B,C,X,Y) VALUES (10, '123456','654321', 10, 15); END
How to: |
A view is a transient object that inherits most of the characteristics of a table. Like a table, it is composed of rows and columns:
Tip: To use a view, issue a SELECT from it. You cannot issue a TABLE request against the view because the view is not extracted as a physical FOCUS data source. To create a HOLD file for extracted data, specify ON TABLE HOLD after the SQL statements. For details on creating HOLD files, see Saving and Reusing Your Report Output.
The SQL Translator supports the following SQL statement:
CREATE VIEW viewname AS subquery ;
where:
Is the name of the view.
Is a SELECT statement that nests inside:
The following example creates a view named XYZ:
SQL CREATE VIEW XYZ AS SELECT CAR, MODEL FROM CAR; END
To report from the view, issue:
SQL SELECT CAR, MODEL FROM XYZ; END
According to normal SQL data definition syntax, each CREATE VIEW statement must terminate with a semicolon.
The following request removes the XYZ view:
SQL DROP VIEW XYZ; END
The SQL Translator automatically generates Cartesian product style answer sets unless you explicitly turn this feature off. However, it is advisable to leave the CARTESIAN setting on, since turning it off does not comply with ANSI standards. For details on the SET CARTESIAN command, see Merging Data Sources.
Continental Decimal Notation displays numbers using a comma to mark the decimal position and periods for separating significant digits into groups of three. This notation is available for SQL Translator requests.
The following example creates a column defined as 1.2 + SEATS:
SET CDN=ON SQL SELECT SEATS + 1,2 FROM CAR; END
Specify fields in an SQL request using:
The following field identifier can be included in a request:
"COUNTRY.NAME"
To qualify the delimited field name COUNTRY.NAME with its file name, use:
CAR."COUNTRY.NAME"
The SQL UNION, INTERSECT, and EXCEPT operators generate MATCH logic. The number of files that can participate is determined by the MATCH limit. UNION with parentheses is supported.
Match logic merges the contents of your data sources. See Merging Data Sources.
The SQL SELECT list, WHERE predicate, and HAVING clause can include numeric constants, literals enclosed in single quotation marks, expressions, and any scalar functions. Internally, a virtual field is created for each of these in the SELECT list. The value of the virtual field is provided in the answer set.