一起使用 GROUP BY 和 PARTITION BY
Using GROUP BY and PARTITION BY together
这是我的第一个查询,returns 下图中的结果
我从 3 个 table 中获取“餐厅名称”、“客户名称”、“订单计数”和“日期”,即“订单”、“客户”和“餐厅”。然后我按餐厅名称、顾客姓名和日期对其进行分组。
SELECT R.name AS name_of_r, C.name AS name_of_c, COUNT(O.id) AS Orders, date,
FROM orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
GROUP BY R.name, C.name, date
name_of_r | name_of_c | Orders | date
Howdy | John Almagro | 1 | 2021-01-07
Howdy | John Almagro | 1 | 2021-07-01
Howdy | Luke Pablo | 1 | 2021-01-01
Howdy | Steven Smith | 1 | 2021-09-01
JFC | Monty Tron | 1 | 2021-01-01
JFC | Steven Smith | 1 | 2021-05-04
JFC | Steven Smith | 1 | 2021-06-01
KFC | Luke Pablo | 1 | 2021-01-09
KFC | Michael Wren | 1 | 2021-01-01
KFC | Monty Tron | 1 | 2021-04-01
KFC | Steven Smith | 1 | 2021-01-01
KFC | Steven Smith | 1 | 2021-03-01
KFC | Steven Smith | 1 | 2021-08-01
Kitchen Cuisine | Luke Pablo | 1 | 2021-01-05
Kitchen Cuisine | Luke Pablo | 1 | 2021-04-01
Kitchen Cuisine | Steven Smith | 1 | 2021-03-01
Kitchen Cuisine | Steven Smith | 1 | 2021-06-01
McDonald's | Arthur Chen | 1 | 2021-01-01
McDonald's | Arthur Chen | 1 | 2021-03-02
McDonald's | Arthur Chen | 1 | 2021-05-03
McDonald's | Arthur Chen | 1 | 2021-07-01
McDonald's | Arthur Chen | 1 | 2021-08-01
McDonald's | Arthur Chen | 1 | 2021-09-01
现在,我想做的是按“name_of_r”(餐厅名称)按“日期”排序,并给每个行号一个行号,这样我就可以得到每个 window.
实际上,这是我的子查询,我希望从中获取在每家餐厅下第 3 个订单的顾客的姓名。
我尝试添加 row_number() 和分区依据,但它没有用,给我一个语法错误
SELECT R.name AS name_of_r, C.name AS name_of_c, COUNT(O.id), date,
ROW_NUMBER() OVER(PARTITION BY R.name ORDER BY date) AS row_num
FROM orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
GROUP BY R.name, C.name, date
我想要的最终输出是在每家餐厅下第 3 份订单的顾客的姓名。喜欢下面的table:
name_of_restaurant | name_of_customer_who_placed_the_3rd_order
Howdy | Luke Pablo
JFC | Steven Smith
KFC | Monty Tron
Kitchen Cuisine | Steven Smith
McDonald's | Arthur Chen
我知道这个,因为我第一次 table 订购日期
我想你需要一个HAVING COUNT
SELECT
R.name AS name_of_r,
C.name AS name_of_c,
COUNT(O.id),
date
FROM
orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
GROUP BY R.name, C.name, date
HAVING COUNT(O.id) >= 3
;
PS: 但对当天下第3单的人有效。否则,日期必须从分组中排除。
更新:
添加了餐厅中每三位顾客的选择请求。
SELECT name_of_r, name_of_c, date
FROM (
SELECT
R.name AS name_of_r,
C.name AS name_of_c,
date,
ROW_NUMBER() OVER (PARTITION BY R.name ORDER BY date) AS nc
FROM
orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
) t
WHERE t.nc = 3
;
这是我的第一个查询,returns 下图中的结果
我从 3 个 table 中获取“餐厅名称”、“客户名称”、“订单计数”和“日期”,即“订单”、“客户”和“餐厅”。然后我按餐厅名称、顾客姓名和日期对其进行分组。
SELECT R.name AS name_of_r, C.name AS name_of_c, COUNT(O.id) AS Orders, date,
FROM orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
GROUP BY R.name, C.name, date
name_of_r | name_of_c | Orders | date
Howdy | John Almagro | 1 | 2021-01-07
Howdy | John Almagro | 1 | 2021-07-01
Howdy | Luke Pablo | 1 | 2021-01-01
Howdy | Steven Smith | 1 | 2021-09-01
JFC | Monty Tron | 1 | 2021-01-01
JFC | Steven Smith | 1 | 2021-05-04
JFC | Steven Smith | 1 | 2021-06-01
KFC | Luke Pablo | 1 | 2021-01-09
KFC | Michael Wren | 1 | 2021-01-01
KFC | Monty Tron | 1 | 2021-04-01
KFC | Steven Smith | 1 | 2021-01-01
KFC | Steven Smith | 1 | 2021-03-01
KFC | Steven Smith | 1 | 2021-08-01
Kitchen Cuisine | Luke Pablo | 1 | 2021-01-05
Kitchen Cuisine | Luke Pablo | 1 | 2021-04-01
Kitchen Cuisine | Steven Smith | 1 | 2021-03-01
Kitchen Cuisine | Steven Smith | 1 | 2021-06-01
McDonald's | Arthur Chen | 1 | 2021-01-01
McDonald's | Arthur Chen | 1 | 2021-03-02
McDonald's | Arthur Chen | 1 | 2021-05-03
McDonald's | Arthur Chen | 1 | 2021-07-01
McDonald's | Arthur Chen | 1 | 2021-08-01
McDonald's | Arthur Chen | 1 | 2021-09-01
现在,我想做的是按“name_of_r”(餐厅名称)按“日期”排序,并给每个行号一个行号,这样我就可以得到每个 window.
实际上,这是我的子查询,我希望从中获取在每家餐厅下第 3 个订单的顾客的姓名。
我尝试添加 row_number() 和分区依据,但它没有用,给我一个语法错误
SELECT R.name AS name_of_r, C.name AS name_of_c, COUNT(O.id), date,
ROW_NUMBER() OVER(PARTITION BY R.name ORDER BY date) AS row_num
FROM orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
GROUP BY R.name, C.name, date
我想要的最终输出是在每家餐厅下第 3 份订单的顾客的姓名。喜欢下面的table:
name_of_restaurant | name_of_customer_who_placed_the_3rd_order
Howdy | Luke Pablo
JFC | Steven Smith
KFC | Monty Tron
Kitchen Cuisine | Steven Smith
McDonald's | Arthur Chen
我知道这个,因为我第一次 table 订购日期
我想你需要一个HAVING COUNT
SELECT
R.name AS name_of_r,
C.name AS name_of_c,
COUNT(O.id),
date
FROM
orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
GROUP BY R.name, C.name, date
HAVING COUNT(O.id) >= 3
;
PS: 但对当天下第3单的人有效。否则,日期必须从分组中排除。
更新: 添加了餐厅中每三位顾客的选择请求。
SELECT name_of_r, name_of_c, date
FROM (
SELECT
R.name AS name_of_r,
C.name AS name_of_c,
date,
ROW_NUMBER() OVER (PARTITION BY R.name ORDER BY date) AS nc
FROM
orders O
INNER JOIN restaurants R ON R.id = O.restaurant_id AND R.country = O.country
INNER JOIN customers C ON C.id = O.customer_id AND C.country = O.country
) t
WHERE t.nc = 3
;