मैंने 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]
1
David Masip 17 मई 2021, 14:02

2 जवाब

सबसे बढ़िया उत्तर

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
1
Sergey Geron 17 मई 2021, 14:08

आपके पास गलत क्रम में खंड हैं। आप जिस चीज की परवाह करते हैं उसके लिए आदेश देना है:

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
2
Gordon Linoff 17 मई 2021, 14:03