Over() 函数不覆盖 table 中的所有行
Over() function does not cover all rows in the table
我正在用 MySQL 练习 SQL,在 SQL 中遇到了一个奇怪的行为。假设我有一个像这样的 table:
Delivery table:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1 | 1 | 2019-08-01 | 2019-08-02 |
| 2 | 5 | 2019-08-02 | 2019-08-02 |
| 3 | 1 | 2019-08-11 | 2019-08-11 |
| 4 | 3 | 2019-08-24 | 2019-08-26 |
| 5 | 4 | 2019-08-21 | 2019-08-22 |
| 6 | 2 | 2019-08-11 | 2019-08-13 |
+-------------+-------------+------------+-----------------------------+
我的查询:
SELECT COUNT(*) as imm, count(*) over() as all_t
FROM
Delivery
WHERE order_date = customer_pref_delivery_date
结果:
+-----+-------+
| imm | all_t |
+-----+-------+
| 2 | 1 |
+-----+-------+
我预计 over() 函数将涵盖整个 table,在本例中 return 6,但它仅 returns 1,出于某种原因。对这种行为有解释吗?
window函数:
count(*) over() as all_t
对查询结果进行操作:
SELECT COUNT(*) as imm
FROM Delivery
WHERE order_date = customer_pref_delivery_date
只有 1 行(1 列),这就是为什么你得到 1 作为结果。
我相信你要找的是条件聚合:
SELECT COUNT(CASE WHEN order_date = customer_pref_delivery_date THEN 1 END) AS imm,
COUNT(*) AS all_t -- here it's the aggregate function COUNT()
FROM Delivery;
参见demo。
SELECT COUNT(*)
统计满足WHERE子句条件的行数。这样的行有两行。
具有聚合函数但没有 GROUP BY returns 单个行的查询,以报告计数。然后 window 函数应用于该结果集,并计算 1 行。换句话说,window 函数应用于行集合 在 聚合减少了行数之后。
您可以通过省略聚合来测试它:
mysql> SELECT count(*) over() as all_t FROM Delivery WHERE order_date = customer_pref_delivery_date;
+-------+
| all_t |
+-------+
| 2 |
| 2 |
+-------+
或者没有 WHERE 子句的条件,它 returns 六行集合中每一行的行数:
mysql> SELECT count(*) over() as all_t FROM Delivery;
+-------+
| all_t |
+-------+
| 6 |
| 6 |
| 6 |
| 6 |
| 6 |
| 6 |
+-------+
我正在用 MySQL 练习 SQL,在 SQL 中遇到了一个奇怪的行为。假设我有一个像这样的 table:
Delivery table:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1 | 1 | 2019-08-01 | 2019-08-02 |
| 2 | 5 | 2019-08-02 | 2019-08-02 |
| 3 | 1 | 2019-08-11 | 2019-08-11 |
| 4 | 3 | 2019-08-24 | 2019-08-26 |
| 5 | 4 | 2019-08-21 | 2019-08-22 |
| 6 | 2 | 2019-08-11 | 2019-08-13 |
+-------------+-------------+------------+-----------------------------+
我的查询:
SELECT COUNT(*) as imm, count(*) over() as all_t
FROM
Delivery
WHERE order_date = customer_pref_delivery_date
结果:
+-----+-------+
| imm | all_t |
+-----+-------+
| 2 | 1 |
+-----+-------+
我预计 over() 函数将涵盖整个 table,在本例中 return 6,但它仅 returns 1,出于某种原因。对这种行为有解释吗?
window函数:
count(*) over() as all_t
对查询结果进行操作:
SELECT COUNT(*) as imm
FROM Delivery
WHERE order_date = customer_pref_delivery_date
只有 1 行(1 列),这就是为什么你得到 1 作为结果。
我相信你要找的是条件聚合:
SELECT COUNT(CASE WHEN order_date = customer_pref_delivery_date THEN 1 END) AS imm,
COUNT(*) AS all_t -- here it's the aggregate function COUNT()
FROM Delivery;
参见demo。
SELECT COUNT(*)
统计满足WHERE子句条件的行数。这样的行有两行。
具有聚合函数但没有 GROUP BY returns 单个行的查询,以报告计数。然后 window 函数应用于该结果集,并计算 1 行。换句话说,window 函数应用于行集合 在 聚合减少了行数之后。
您可以通过省略聚合来测试它:
mysql> SELECT count(*) over() as all_t FROM Delivery WHERE order_date = customer_pref_delivery_date;
+-------+
| all_t |
+-------+
| 2 |
| 2 |
+-------+
或者没有 WHERE 子句的条件,它 returns 六行集合中每一行的行数:
mysql> SELECT count(*) over() as all_t FROM Delivery;
+-------+
| all_t |
+-------+
| 6 |
| 6 |
| 6 |
| 6 |
| 6 |
| 6 |
+-------+