Input: Have got Table '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
Scenario: Have to take average of Sales grouped by Store,Category and save in separate column as well as pick top 3 sales day and save in separate column. As a result, one row for one store,category as shown below in sample output.
Expected Output:
Store Category AvgSales PeakDay1 PeakDay2 PeakDay3
11 aaa 1.92 Friday Thursday Tuesday
22 bbb 1.47 Friday Tuesday Monday
Tried Query:
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
Thanks in Advance!