输入:有表'a'
Store Category Sales Day
11 aaa 1.5 Sunday
11 aaa 0.5 Monday
11 aaa 2.5 Tuesday
11 aaa 2.0 Wednesday
11 aaa 3.0 Thursday
11 aaa 3.5 Friday
11 aaa 0.5 Saturday
22 bbb 0.5 Sunday
22 bbb 1.5 Monday
22 bbb 2.3 Tuesday
22 bbb 0.3 Wednesday
22 bbb 1.4 Thursday
22 bbb 4.1 Friday
22 bbb 0.2 Saturday
场景:必须取按门店、类别分组的销售平均值,并在单独的列中保存,同时选择前3个销售日并在单独的列中保存。结果,一行对应一个商店,分类如下所示。
预期产量:
Store Category AvgSales PeakDay1 PeakDay2 PeakDay3
11 aaa 1.92 Friday Thursday Tuesday
22 bbb 1.47 Friday Tuesday Monday
已尝试查询:
SELECT
Store,
Category,
avg(Sales) as AvgSales,
ARRAY_AGG(Sales ORDER BY Sales DESC LIMIT 3) #but this line will not produce results in 3 separate columns
FROM A
GROUP BY Site, Category
提前谢谢!
2条答案
按热度按时间amrnrhlw1#
下面是bigquery标准sql
如果要应用到问题输出的样本数据
z9zf31ra2#
你可以使用数组agg,但是
row_number()
看起来很简单:如果要将其放入数组列中,可以使用: