PostgreSQL - with clause

In PostgreSQL, WITH queries provide a method for the preparation of the statement of aid for use in larger query. It helps to break down large, complex query into simpler forms, easy to read. These statements are usually called common table expressions (Common Table Expressions) or cte, they can be considered only a temporary table defines the query exists.

WITH CTE query is a query, particularly useful when multiple sub-queries executed. It also helps to replace temporary table. It calculates the polymerization time, and allows us to pass its name in the query (possibly multiple) refer to it. Supports select, delete, update, insert statement.

WITH clause must be defined before use in the query.

The basic syntax WITH query is as follows:

WITH
   name_for_summary_data AS (
      SELECT Statement)
   SELECT columns
   FROM name_for_summary_data
   WHERE conditions <=> (
      SELECT column
      FROM name_for_summary_data)
   [ORDER BY columns]

 Which is the name of name_for_summary_data WITH clause. name_for_summary_data existing table may be the same name, and has priority.

You can use data modification statements (insert, update, or delete) in the WITH. This allows you to perform a number of different operations in the same query.

 

Recursive with statement

Recursive query or query level is a form of CTE, CTE can reference itself, namely, WITH query can refer to its own output, it can be recursive.

Example: Consider the following table record company:

testdb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen | 25 | Texas | 15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark | 25 | Rich Moon | 65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

 Now, let's write a query using the WITH clause to select records from the table above, as follows:

With CTE AS
(Select
 ID
, NAME
, AGE
, ADDRESS
, SALARY
FROM COMPANY )
Select * From CTE;

 PostgreSQL statement given above yields the following results:

id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen | 25 | Texas | 15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark | 25 | Rich Moon | 65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

 

Now, let's use the RECURSIVE clause with keywords and write a query to find the total amount of wages less than the sum of wages 20,000, as follows:

WITH RECURSIVE t(n) AS (
   VALUES (0)
   UNION ALL
   SELECT SALARY FROM COMPANY WHERE SALARY < 20000
)
SELECT sum(n) FROM t;

where n represents the column_list (if you do not write, default is returned in parentheses after as the column)

PostgreSQL statement given above yields the following results:

  sum
-------
 25000
(1 row)

 

Let us use data modification statements and clauses are written with a query as follows:

First, create a table similar to Table COMPANY1 of COMPANY. Example query effectively from row to move COMPANY COMPANY1. WITH delete the specified delete the row from the company, return to their contents by returning clause; then read the output of the main query and insert COMPANY1 table:

CREATE TABLE COMPANY1(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

WITH moved_rows AS (
   DELETE FROM COMPANY
   WHERE
      SALARY >= 30000
   RETURNING *
)
INSERT INTO COMPANY1 (SELECT * FROM moved_rows);

 PostgreSQL statement given below will produce the following results:

INSERT 0 3

 Now, records and tables COMPANY company1 as follows:

testdb=# SELECT * FROM COMPANY;
 id | name  | age |  address   | salary
----+-------+-----+------------+--------
  1 | Paul  |  32 | California |  20000
  2 | Allen | 25 | Texas | 15000
  3 | Teddy |  23 | Norway     |  20000
  7 | James |  24 | Houston    |  10000
(4 rows)


testdb=# SELECT * FROM COMPANY1;
 id | name  | age | address | salary
----+-------+-----+-------------+--------
  4 | Mark | 25 | Rich Moon | 65000
  5 | David |  27 | Texas       |  85000
  6 | Kim   |  22 | South-Hall  |  45000
(3 rows)

 

CTE some of the benefits:

1. To improve the readability of complex queries. You can use cte in a more organized and readable way of organizing complex queries.

2. The ability to create recursive queries. A recursive query is a reference to their queries. When you want to query hierarchical data (such as an organization chart or BOM), recursive queries is very convenient.

3. The window function used in conjunction with. You can use a combination CTEs and window function to create an initial result set, and use another select statement to further process the result set.

 

Reference address: https: //www.tutorialspoint.com/postgresql/postgresql_with_clause.htm

 

Guess you like

Origin www.cnblogs.com/abclife/p/11022540.html