我在BigQuery中有这个查询:
SELECT
ID,
max(amount) as money,
STRFTIME_UTC_USEC(TIMESTAMP(time), '%j') as day
FROM table
GROUP BY day
控制台显示错误,因为它希望ID到group by子句,但如果我在组中添加ID,则会在特定日期获得许多ID。
我想打印一个特定日期内最大金额的唯一ID。
例如:
ID: 1 Money:123 Day:365
答案 0 :(得分:0)
从问题中不清楚,但看起来你在特定日期每个给定ID只有一个条目。假设这样,下面的查询可以满足您的需求
SELECT id, amount, day
FROM (
SELECT
id, amount, day,
ROW_NUMBER() OVER(PARTITION BY day ORDER BY amount DESC) AS win
FROM dataset.table
)
WHERE win = 1
如果上述假设错误(因此您在同一天有相同ID的多个条目),请使用以下
SELECT id, amount, day
FROM (
SELECT id, amount, day,
ROW_NUMBER() OVER(PARTITION BY day ORDER BY amount DESC) AS win
FROM (
SELECT id, SUM(amount) AS amount, day
FROM dataset.table
GROUP BY id, day
)
)
WHERE win = 1