Dynamic Pivot Columns | How to generate dynamic Pivot Columns in BigQuery

Dynamic Pivot Columns | How to generate dynamic Pivot Columns in BigQuery

Datasudh

2 года назад

8,758 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@ClaudioDeLucke
@ClaudioDeLucke - 24.07.2023 08:49

How could i connect this to sheets?

Ответить
@user-vy1py7io7s
@user-vy1py7io7s - 24.07.2023 08:09

Great Video!

Ответить
@LorencoGonzaga
@LorencoGonzaga - 21.02.2023 16:29

Awesome! Congrats for the videos! Keep posting to the BigQuery series please!!! Cheers

Ответить
@TechnologiesExtendo
@TechnologiesExtendo - 03.02.2023 20:23

Thank you much for the tutorial! Just what I need it. Now, how can you create an new table from the results in a way you can do it scheduled? I believe that would be using STORE PROCEDUREs with this scripting, but I don't know what would be the syntax to store the results into a final TABLE that can be reconstructed every time I run the PROCEDURE. I tried using SCHEDULED QUERIES but the scripting is not allowed there. Any help would be much appreciated.

Ответить
@lxvi4322
@lxvi4322 - 21.12.2022 12:26

Thansk,
can I use this with a CTE?

Ответить
@farsikogama6114
@farsikogama6114 - 15.08.2022 15:39

I get an error. Invalid format specifier character "R" in FORMAT string. Anyone can help?

DECLARE MONTHS STRING;
SET MONTHS = (
SELECT
CONCAT('("', STRING_AGG(DISTINCT cast(MONTH as string), '", "'), '")'),
FROM (
select distinct date_trunc(jakarta_data_date, month) MONTH,
from `data-gojek-id-mart.corporate_strategy.detail_booking` a
where jakarta_data_date >= date_sub(date_trunc(current_date('Asia/Jakarta'), month), interval 3 month)
order by 1
)
);

select MONTHS;


EXECUTE IMMEDIATE format("""
SELECT * FROM
(
select
date_trunc(jakarta_data_date, month) AS MONTH,
sa.service_area_name AS CITY,
regexp_extract(UPPER(a.service_type_name),'(CAR|RIDE|FOOD)') as service_type_name,
count(distinct order_no) AS BOOKING,

from `data-gojek-id-mart.corporate_strategy.detail_booking` a
left join `data-gojek-id-presentation.location.dim_service_area` sa on a.service_area_id = sa.service_area_id

where jakarta_data_date >= date_sub(date_trunc(current_date('Asia/Jakarta'), month), interval 3 month)
and (upper(a.service_type_name) like '%RIDE%' OR upper(a.service_type_name) like '%CAR%' OR upper(a.service_type_name) like '%FOOD%')
and upper(sa.service_area_name) = 'BANDUNG'

group by 1,2,3

)

PIVOT
(SUM(BOOKING) FOR MONTH in(%s))""", MONTHS);

Ответить
@agustinnosiglia5848
@agustinnosiglia5848 - 15.08.2022 06:01

Exactly what I was looking for with an easy-to-understand example! Thanks!

Ответить
@hasanasim3980
@hasanasim3980 - 15.08.2022 03:08

Thank you for the wonderful explanation. Is there a similar way by which we can dynamically aggregate string columns in bigquery sql? Usually we would just use Select STRING_AGG(col_1), col_2 From table1 Group BY col_2. What if the number of columns are increasing dynamically?

Ответить
@yuejian8089
@yuejian8089 - 27.07.2022 05:20

Thank you!!!! It reallly helps a lot.

Ответить
@alelust7170
@alelust7170 - 21.03.2022 19:47

Tks a lot!

Ответить