Hi I want to aggregate group by and change it from tall to wide data in bigquery, how do I do so? I have a lot of sources and here's a sample data.
Here's the table
date | source | price | id |
---|---|---|---|
2022-01-01 | A | 2 | 1 |
2022-01-02 | A | 2 | 1 |
2022-01-03 | A | 4 | 1 |
2022-01-04 | A | 4 | 1 |
2022-01-01 | B | 1 | 1 |
2022-01-02 | B | 1 | 1 |
2022-01-03 | B | 3 | 1 |
2022-01-04 | B | 3 | 1 |
2022-01-01 | A | 2 | 2 |
2022-01-02 | A | 2 | 2 |
2022-01-03 | A | 4 | 2 |
2022-01-04 | A | 4 | 2 |
2022-01-01 | B | 1 | 2 |
2022-01-02 | B | 1 | 2 |
2022-01-03 | B | 3 | 2 |
2022-01-04 | B | 3 | 2 |
into fields of min from price from all source for group by id and min price per source group by id
id | minPrice | minPriceSourceA | minPriceSourceB |
---|---|---|---|
1 | 2.5 | 3 | 2 |
2 | 2.5 | 3 | 2 |
Here's my current code
with Amin as
(
select
id,source,
min(price) min price
from table
where source ="A"
group by 1,2
),
Bmin as
(
select
id,source,
min(price) min price
from table
where source ="B"
group by 1,2
),
select
t1.id,t1.minprice,
Amin.minprice minPriceSourceA,
Bmin.minprice minPriceSourceB
from(
select
id,source,
min(price) minprice
from table
group by 1,2) t1
left join Amin on t1.id=Amin.id
left join Bmin on t1.id=Bmin.id
The problem is I have over 100 sources and id, if I do query manually the code will be very long. Is there an efficient way to do this?