使用子查询中的LIMIT简化查询,并在子查询和外部查询中复制WHERE子句

问题描述 投票:1回答:1

SQL Fiddle

假设以下表格:

customers

+----+------------------------+------------+------------+
| id | company                | first_name | last_name  |
+----+------------------------+------------+------------+
|  1 | MDD                    | T          | H          |
|  2 | Aliance Magnet A LLP   | A          | Wilkinson  |
|  3 | MAF                    | C          | G          |
|  4 | QL                     | F          | B          |
|  5 | ARL                    | S          | P          |
|  6 | Q Corp.                | H          | H          |
|  7 | VQDA                   | L          | W          |
|  8 | AESC                   | E          | W          |
|  9 | Placement Incorporated | C          | Mendez     |
| 10 | Numpties United        | Y          | Cunningham |
+----+------------------------+------------+------------+

transactions

+----+-----------+-------------+------+
| id | form_type | customer_id | due  |
+----+-----------+-------------+------+
|  1 | invoice   |           9 |    1 |
|  2 | payment   |           1 |    6 |
|  3 | invoice   |           7 |    9 |
|  4 | payment   |           9 |    4 |
|  5 | invoice   |           7 |    5 |
|  6 | payment   |           3 |    5 |
|  7 | invoice   |           9 |    5 |
|  8 | invoice   |           9 |   10 |
|  9 | invoice   |          10 |    1 |
| 10 | invoice   |           2 |    4 |
+----+-----------+-------------+------+

以下查询查找发票事务> 0的客户,但仅返回与前三个客户相关的记录。

SELECT
  t.id AS trans_id,
  c.id AS customer_id,
  c.company,
  c.first_name,
  c.last_name,
  t.due
FROM (
  SELECT DISTINCT c.*
  FROM customers AS c
  INNER JOIN transactions AS t ON t.customer_id = c.id
  WHERE t.due > 0
  AND t.form_type = 'invoice'
  ORDER BY c.company, c.first_name, c.last_name
  LIMIT 3
) AS c
INNER JOIN transactions AS t ON t.customer_id = c.id
WHERE t.due > 0
AND t.form_type = 'invoice'
ORDER BY c.company, c.first_name, c.last_name;

results

+----------+-------------+------------------------+------------+------------+------+
| trans_id | customer_id | company                | first_name | last_name  | due  |
+----------+-------------+------------------------+------------+------------+------+
|       10 |           2 | Aliance Magnet A LLP   | A          | Wilkinson  |    4 |
|        9 |          10 | Numpties United        | Y          | Cunningham |    1 |
|        1 |           9 | Placement Incorporated | C          | Mendez     |    1 |
|        7 |           9 | Placement Incorporated | C          | Mendez     |    5 |
|        8 |           9 | Placement Incorporated | C          | Mendez     |   10 |
+----------+-------------+------------------------+------------+------------+------+

有没有办法,例如使用窗口函数或公用表表达式,避免在内部和外部查询中复制WHERE t.due > 0 AND t.form_type = 'invoice'ORDER BY子句?或者使用单个SQL查询获得相同结果的其他方法?

sql postgresql postgresql-9.6
1个回答
2
投票

你可以使用DENSE_RANK

WITH cte AS (
  SELECT t.id AS trans_id,
         c.id AS customer_id,
         c.company,
         c.first_name,
         c.last_name,
         t.due,
         DENSE_RANK() OVER(ORDER BY c.company, c.first_name, c.last_name) rn
  FROM customers AS c
  JOIN transactions AS t ON t.customer_id = c.id
  WHERE t.due > 0 AND t.form_type = 'invoice'
)
SELECT * FROM cte WHERE rn <= 3;

DBFiddle Demo

© www.soinside.com 2019 - 2024. All rights reserved.