Pat Pat - 6 days ago 5
SQL Question

Oracle SQL group by

I have below data in my table,

Id month value

1 Jun 20
1 Jul 22
1 Aug 0
1 Sep 12
2 Jun 21
2 Jul 45


I need to group them as

Id Jun Jul Aug Sep
1 20 22 0 12
2 21 45


I am not sure what syntax i should use. Help appreciated

Answer

@Gurwinder's answer is workable. Also you can use pivot (in Oracle 11 or above):

select *
 from (
  select id, month, value
    from your_table    
)
pivot (
      max(value)
      for month in ('Jun' as "Jun", 'Jul' as "Jul", 'Aug' as "Aug", 'Sep' as "Sep")
)t