Skip to content
Advertisement

Pivot rows into column Googlesql

I have a data like below

ID  name  timeSpent_in_mins  
1   ABC   12
1   XYZ   24

I need something like

ID  ABC  XYZ
1  12   24

Need something similar to Pivot in googlesql/standard sql

Thanks In Advance

Advertisement

Answer

You can use Conditional Aggregation :

select ID,
       sum(IF (name='ABC', timeSpent_in_mins, 0)) as ABC,
       sum(IF (name='XYZ', timeSpent_in_mins, 0)) as XYZ
  from tab
 group by ID
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement