在 BigQuery 中限定分组依据
Qualify group by in BigQuery
我已经开始在 BigQuery 中使用 qualify
,但 group by
运算符出现问题。
这可以正常工作(尽管我必须输入 where true
这有点奇怪):
WITH Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT *
FROM Produce
WHERE true QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
但这不起作用:
WITH Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT product, count(*)
FROM Produce
WHERE true QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
group by 1
我是不是做错了什么?抛出的错误是
Syntax error: Expected end of input but got keyword GROUP at [13:1]
您的子句顺序错误。您关心的顺序是:
FROM
WHERE
GROUP BY
HAVING
QUALIFY
所以,试试这个:
SELECT product, count(*)
FROM Produce
WHERE true
GROUP BY 1
QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
qualify
预计在 group by
:
之后
WITH Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT product, count(*)
FROM Produce
WHERE true
group by product, quarter
QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
我已经开始在 BigQuery 中使用 qualify
,但 group by
运算符出现问题。
这可以正常工作(尽管我必须输入 where true
这有点奇怪):
WITH Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT *
FROM Produce
WHERE true QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
但这不起作用:
WITH Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT product, count(*)
FROM Produce
WHERE true QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
group by 1
我是不是做错了什么?抛出的错误是
Syntax error: Expected end of input but got keyword GROUP at [13:1]
您的子句顺序错误。您关心的顺序是:
FROM
WHERE
GROUP BY
HAVING
QUALIFY
所以,试试这个:
SELECT product, count(*)
FROM Produce
WHERE true
GROUP BY 1
QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1
qualify
预计在 group by
:
WITH Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT product, count(*)
FROM Produce
WHERE true
group by product, quarter
QUALIFY RANK() OVER (PARTITION BY product ORDER BY quarter DESC) <= 1