I would like to get dates (monthYear,weekYear,etc.) as columns, and having the value as count activities by users, but i'm not reaching it :(
Some example:
- My table
userid | activityId | activityStatus | activityDate |
---|---|---|---|
A1 | z1 | finished | 2022-08-01T15:00:00 |
A2 | z2 | finished | 2022-08-07T20:00:00 |
A2 | z3 | finished | 2022-08-08T10:00:00 |
A1 | z4 | finished | 2022-09-17T16:00:00 |
A1 | z5 | finished | 2022-09-20T17:00:00 |
A3 | z6 | finished | 2022-08-19T13:00:00 |
What I'im trying to do, something like this (but I know now that's not working):
SELECT
userid,
COUNT(activityId) as doneActivities,
CONCAT(EXTRACT(YEAR from activityDate),'-',EXTRACT(WEEK from activityDate))
And the result of this attemp is like:
userid | doneActivities | weekYear |
---|---|---|
A1 | 1 | 31-2022 |
A1 | 2 | 33-2022 |
A2 | 1 | 31-2022 |
A2 | 1 | 32-2022 |
A3 | 1 | 33-2022 |
- Expected result would be something like this:
userid | 31-2022 | 32-2022 | 33-2022 |
---|---|---|---|
A1 | 1 | 0 | 2 |
A2 | 1 | 1 | 0 |
A3 | 0 | 0 | 1 |
I know how to do it on Power BI, but I want to automate this for futures queries.
If it's not clear, please let me know guys, and I'll try to explain again. There is some time that I don't practice english.
Thanks in advance!
CodePudding user response:
Since BigQuery doesn't support
CodePudding user response:
Just to add on to the great answer of @JihoChoi, if you would like to implement dynamic pivoting. See approach below:
create temp table my_table (userid string,doneActivities int64,weekYear string);
insert into my_table
select
userid,
count(distinct activityId) as doneActivities,
weekYear
from
(
select
*,
concat('_',extract(YEAR from activityDate),'_',extract(WEEK from activityDate)) as weekYear
from `project-id.dataset_id.table_id`
)
group by userid,weekYear;
execute immediate (
select '''
select * from my_table
pivot(sum(doneActivities) for weekYear in ("''' || string_agg(weekYear,'", "') || '''"))
'''
from (select * from my_table order by weekYear)
)
Output: