Identifiers, literals, and functions can be combined into expressions. Expressions can be used almost anywhere in a query: SELECT, FROM (if specifying join criteria), WHERE, GROUP BY, HAVING, or ORDER BY. Show
The Data Virtuality Server supports the following types of expressions:
Column IdentifiersColumn identifiers are used to specify the output columns in SELECT statements, the columns and their values for INSERT and UPDATE statements, and criteria used in WHERE and FROM clauses. They are also used in GROUP BY, HAVING, and ORDER BY clauses. The syntax for column identifiers was defined in the Identifiers section above. LiteralsLiteral values represent fixed values. These can be any of the 'standard' data types. Syntax Rules:
Some examples of simple literal values
Date/Time Literals can use JDBC Escaped Literal Syntax or the ANSI keyword syntax:
Using Unicode charactersThere's a possibility to specify any Unicode characters (with codes 0-65535) in SQL strings. Normally if you try to include special characters in the usual way using backslash, it will be taken literally by DV. To use special characters you should use a 'E' letter right before the first apostrophe character bounding a string constant. Actually, the syntax supports the following codes: \b, \t, \n, \f, \r and also the standard way to specify a Unicode character: \uXXXX where X is a hexadecimal digit. Examples:
ArraysAn array is an ordered list of elements of a single data type. Each element has its own index value by which it can be accessed. Defining ArraysGeneral Array Syntax(expr, expr ... [,]) ARRAY(expr, ...) SQL Empty ArraysSingle Element ArraysUsing ArraysHere are some things to keep in mind when working with arrays:
And here's how to define and use an array: BEGIN // Defining an array DECLARE OBJECT favourite_birds = ARRAY('seagull', 'eagle', 'nightingale', 'sparrow'); // Using the array SELECT favourite_birds[2] END // Output eagle SQL The Data Virtuality Server also has special functions for working with arrays: Array Functions. Aggregate FunctionsAggregate functions take sets of values from a group produced by an explicit or implicit GROUP BY and return a single scalar value computed from the group. Data Virtuality Server supports the following aggregate functions:
Syntax RulesSome aggregate functions may contain the keyword 'DISTINCT' before the expression, indicating that duplicate expression values should be ignored. DISTINCT is not allowed in COUNT(*) and is not meaningful in MIN or MAX (result would be unchanged), but it can be used in COUNT, SUM, and AVG. Some other things to keep in mind when working with aggregate functions:
Window FunctionsThe Data Virtuality Server supports ANSI SQL 2003 window functions. A window function allows an aggregate function to be applied to a subset of the result set, without the need for a GROUP BY clause. A window function is similar to an aggregate function, but requires the use of an OVER clause or window specification. Usageaggregate|ranking OVER ([PARTITION BY ...] [ORDER BY ...] [<FRAME-CLAUSE>]) CODE aggregate can be any aggregate function. Ranking can be one of ROW_NUMBER() , RANK(), DENSE_RANK(). Syntax Rules
Frame ClauseThe frame clause allows the user to specify a dynamic group of rows, or a range, inside the partition (analogous to a sliding frame in a window). It can be used in two ways:
Where frame_start and frame_end can be one of:
Restrictions are that frame_start cannot be UNBOUNDED FOLLOWING, frame_end cannot be UNBOUNDED PRECEDING, and the frame_end choice cannot appear earlier in the above list than the frame_start choice. For example, RANGE BETWEEN CURRENT ROW AND value PRECEDING is not allowed. Examples of UsageLet's assume we have following table called my_table: Examples of Frame Clause usage SELECT x, SUM(y) OVER (PARTITION BY y ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS window_column FROM data_source.my_table; -- Result: -- |-----------|---------------| -- | x | window_column | -- |-----------|---------------| -- | 1 | 1 | -- | 2 | 2 | -- | 3 | 3 | -- | 4 | 2 | -- | 5 | 3 | -- |-----------|---------------| SELECT x, COUNT(y) OVER (PARTITION BY y RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS window_column FROM data_source.my_table; -- Result: -- |-----------|---------------| -- | x | window_column | -- |-----------|---------------| -- | 1 | 3 | -- | 2 | 3 | -- | 3 | 3 | -- | 4 | 1 | -- | 5 | 1 | -- |-----------|---------------| SELECT x, y*100/SUM(y) OVER (PARTITION BY y RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS window_column FROM data_source.my_table; -- Result: -- |-----------|---------------| -- | x | window_column | -- |-----------|---------------| -- | 1 | 33 | -- | 2 | 33 | -- | 3 | 33 | -- | 4 | 100 | -- | 5 | 100 | -- |-----------|---------------|SQL Function Definitions
ProcessingWindow functions are logically processed just before creating the output from the SELECT clause. Window functions can use nested aggregates if a GROUP BY clause is present. The is no guaranteed effect on the output ordering from the presence of window functions. The SELECT statement must have an ORDER BY clause to have a predictable ordering. The Data Virtuality Server will process all window functions with the same window specification together. In general, a full pass over the row values coming into the SELECT clause will be required for each unique window specification. For each window specification, the values will be grouped according to the PARTITION BY clause. If no PARTITION BY clause is specified, then the entire input is treated as a single partition. The output value is determined based upon the current row value, it's peers (that is rows that are the same with respect to their ordering), and all prior row values based upon ordering in the partition. The ROW_NUMBER function will assign a unique value to every row regardless of the number of peers. Example Windowed Results SELECT name, salary, max(salary) over (partition by name) as max_sal, rank() over (order by salary) as rank, dense_rank() over (order by salary) as dense_rank, row_number() over (order by salary) as row_num FROM data_source.employeesSQL
See AlsoDuplicate Removal with ROW_NUMBER() and PARTITION to see how window functions can be used for removing duplicates Case and Searched CaseThe Data Virtuality Server supports two forms of the CASE expression which allows conditional logic in a scalar expression. The supported forms are the following: CASE <expr> ( WHEN <expr> THEN <expr>)+ [ELSE expr] END CASE ( WHEN <criteria> THEN <expr>)+ [ELSE expr] END Each form allows for an output based on conditional logic. The first form starts with an initial expression and evaluates WHEN expressions until the values match, and outputs the THEN expression. If no WHEN is matched, the ELSE expression is output. If no WHEN is matched and no ELSE is specified, a null literal value is output. The second form (the searched case expression) searches the WHEN clauses, which specify arbitrary criteria to evaluate. If any criteria evaluates to true, the THEN expression is evaluated and output. If no WHEN is true, the ELSE is evaluated or NULL is output if none exists. Scalar SubqueriesSubqueries can be used to produce a single scalar value in the SELECT, WHERE, or HAVING clauses only. A scalar subquery must have a single column in the SELECT clause and should return either 0 or 1 row. If no rows are returned, null will be returned as the scalar subquery value. For other types of subqueries, see the Subqueries section below. Parameter ReferencesParameters are specified using the '?' symbol. Parameters may only be used with PreparedStatement or CallableStatements in JDBC. Each parameter is linked to a value specified by 1-based index in the JDBC API. Which keyword Cannot be used with subquery that includes GROUP BY?The DISTINCT keyword can't be used with subqueries that include `GROUP BY.
Which of the following functions can be used without a GROUP BY clause?Which of the following functions can be used without GROUP BY clause in SELECT query? Answer: A, B, C, D. All the listed group functions can be used in a query provided no other columns are selected in the SELECT query.
Which of the following can be used only with group functions?The functions MAX, MIN and AVG can be used as GROUP BY functions.
Which of the following is not aggregate group function?Which of the following is not a built in aggregate function in SQL? Explanation: SQL does not include total as a built in aggregate function. The avg is used to find average, max is used to find the maximum and the count is used to count the number of values. 2.
|