SQL queries
SQL grammar
- FactoryTalk Optix supports only a subset of standard SQL.
- The simplified standard SQL grammar best fits to the top-down parser implementation.
- The simplified standard SQL grammar facilitates the readability of the resulting AST.
Statements
-
Use the
SELECTstatement to select data from a database. This statement supports both column names and value expressions, as well as theALLandDISTINCTset quantifiers.Examples:SELECT *, FROM Table1 SELECT *, Timestamp AS T FROM Table1 SELECT Column1 FROM Table1 SELECT *, 10 FROM Table1 SELECT 10, * FROM Table1 SELECT *, 'text value' FROM Table1 SELECT COUNT(*) FROM Table1 SELECT DINSTINCT Column1 FROM Table1Note: TheCASEandWHENexpressions are not supported.Important:The SQL standard does not allow combining
*with derived columns.SELECT *, Timestamp FROM Table1SELECT *, ROW_NUMBER() OVER (ORDER BY Timestamp) FROM DataLoggerFactoryTalk Optix supports this standard to allow any query manipulation for Sparkline charts.
-
Use the
DELETEstatement to delete any existing records in a table.Examples:SELECT * FROM Table1 WHERE ... DELETE FROM Table1 WHERE ... UPDATE Table1 SET column1 = 10 WHERE column2 > 20 ... -
Use the
UPDATEstatement to modify any existing records in a table.Examples:SELECT * FROM Table1 WHERE ... DELETE FROM Table1 WHERE ... UPDATE Table1 SET column1 = 10 WHERE column2 > 20 ...
-
Use the
CREATEstatement to create tables or tables in databases. This statement partially supportsTABLEandINDEX.Examples:CREATE UNIQUE INDEX Table_Index ON Table1 (Column1) CREATE INDEX IF NOT EXISTS Table_Index ON Table1 (Column1)CREATE TABLE TempTable AS SELECT Column1 FROM MyTable WHERE Column1 > 5 CREATE TEMPORARY TABLE TempTable AS SELECT Column1 FROM MyTable WHERE Column1 > 5CREATE TEMPORARY TABLE Table2 AS SELECT Column1 FROM Table1 WHERE Column1 > 5 CREATE UNIQUE INDEX Column1_Index ON Table1 (Column1) DROP TABLE Table1 -
The
DROPstatement drops any existing databases or tables in databases.Examples:DROP TABLE TemporaryTable DROP TABLE Table1
Literal values
| Literal value type | Example | Notes |
|---|---|---|
|
Integral |
|
Do not use thousands separators. Use the Int32 type. |
|
Real |
|
Use Use the Double type. |
|
Real scientific |
|
|
|
Boolean |
|
Use lowercase for Boolean values. |
|
String |
|
Use single quotes to delimit text. Insert a single quote ( |
|
Date and time |
|
Use the ISO8601 format delimited by double quotes. The date and time notation is non-standard ANSI SQL used to avoid the |
|
Regular identifier |
|
Do not use spaces. Start with an uppercase or a lowercase letter. You can use numeric digits, letters, and the underscore character |
|
Delimited identifier |
|
Use double quotes ( In the identifier, use any character except for the Any spaces at the end of the identifier are ignored. |
Join
INNER, UNION, LEFT, RIGHT, FULL, NATURAL and CROSS is supported.
Aliases
You can use aliases in the identifiers, derived column subqueries, and tables.
SELECT Timestamp AS T FROM Table1 WHERE ...
SELECT * FROM Table1 AS T WHERE ...
SELECT * FROM (SELECT * FROM Table1) AS SubQuery WHERE ...
CREATE TABLE Table1 AS SELECT Timestamp, Column1 FROM Table2
Table references
The FROM clause supports the following table references.
| Reference | Example |
|---|---|
|
Fully qualified reference |
|
|
Sub query |
|
... WHERE Table1.Column1 > 10...
WHERE Table1."order id" > 10
SELECT * FROM Table1 AS T WHERE T."order id" > 10
... WHERE "my table"."order id" > 10
Operators
| Operator | Example |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The |
ANY and ALL operators are not supported.Window functions
| Function | Example |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PARTITION BY clause supports only column identifiers.Tokens
| Token | Regular expression |
|---|---|
regular_identifier |
[a-zA-Z][a-zA-Z0-9_]* |
delimited_identifier |
".*?" |
real |
|
boolean |
|
|
Other trivial tokens |
Regex for single char or single-word tokens |
delimited_identifier. The semantic analyzer extracts any date and time values from the delimited_identifier according to the r regular expression.