WITH AS query

Overview

The WITH AS phrase, also called subquery factoring, is used to define a SQL fragment that will be used by the entire SQL statement. This statement is regarded as a common table expression (CTE, common table expression), for example with A as (select * from class) select * from A, first execute to select * from classget a result, record as A, and then execute the select * from Astatement. The A table is just an alias. For large quantities of SQL data, it plays an optimized role.

with summary

  1. Using the with clause allows sub-queries to reuse the same with query block, which is called by select (the with clause can only be referenced by the select query block), generally when the with query is used multiple times. Defined before the quoted select statement, the same level can only be defined with keyword can only be used once, separated by commas.
  2. The return result of the with clause is stored in the user's temporary table space, and only one query is performed, and it is used repeatedly to improve efficiency.
  3. When there are multiple query definitions before select at the same level, use with for the first one, and not with with for the subsequent ones, separated by commas.
  4. There can be no comma between the last with clause and the following query, and it is only separated by the closing parenthesis. The query of the with clause must be enclosed in parentheses
  5. If the with clause is defined and not used in the query, an ora-32035 error will be reported: the query name defined in the with clause is not quoted. (The name of at least one with query is not quoted. The solution is to remove the unquoted with query). As long as there is a quote later, it does not have to be quoted in the main query. The latter with query quote is also possible
  6. The query defined by the previous with clause can be used in the later with clause. But a with clause cannot be nested within a with clause.
  7. When a query block name is the same as a table name or other object, the parser searches from the inside out, and the sub-query block name is used first.
  8. The result of the with query lists aliases, and aliases or must be used when quoting *.

Expand

Different SQL dialects have different support for with as.

clickhouse

clickhouse-with-as
CK provides the WITH keyword to support CTE, and the WITH result clause can be used in the rest of the SELECT query.

CK with aslimitations

  1. Does not support recursive query
  2. When using a subquery in a section, its result should be a scalar with only one row
  3. The result of Expression is not available in the subquery

Example

  1. Use constant expression as variable
WITH '2019-08-01 15:23:00' as ts_upper_bound
SELECT * FROM hits WHERE EventDate = toDate(ts_upper_bound) AND EventTime <= ts_upper_bound
  1. Eject the result of sum(bytes) expression from the SELECT clause list
WITH sum(bytes) as s
SELECT formatReadableSize(s), table
FROM system.parts GROUP BY table ORDER BY s
  1. Use the result of scalar subquery
/* this example would return TOP 10 of most huge tables */
WITH
    (
        SELECT sum(bytes)
        FROM system.parts
        WHERE active
    ) AS total_disk_usage
SELECT (sum(bytes) / total_disk_usage) * 100 AS table_disk_usage, table
FROM system.parts GROUP BY table ORDER BY table_disk_usage DESC LIMIT 10;
  1. Reuse expressions in subqueries
    As a solution to the current limitations of expressions in subqueries,
WITH ['hello'] AS hello
SELECT hello, * FROM
(
    WITH ['hello'] AS hello
    SELECT hello
)

PostgreSQL

PostgreSQL also has complete support for with as, such as supporting multiple with clauses. Examples:

with
u1 as (select id, sum(amount) as num from pay where pay_time >= 1493568000 and pay_time < 1494172800 group by id),
u2 as(select id, sum(amount) as total from pay where pay_time < 1494172800 group by id)
select u1.id, pinfo.sid, u1.num, u2.total from u1, u2, pinfo
where u2.id = u1.id and pinfo.id = u1.id;

Analysis:
Use with to query the id of the player recharged within a fixed time and the total recharge during the period, use with to query the id of the player recharged before the specified time and the total historical recharge, and then query the data in u2 corresponding to all ids in u1, and The sid in pinfo.

reference

SQL with as usage

Guess you like

Origin blog.csdn.net/lonelymanontheway/article/details/107621579