question

SumanGupta-8512 avatar image
0 Votes"
SumanGupta-8512 asked NaomiNNN answered

Pivot rows to columns for multiple columns

Hi, i need the below source data to be pivoted(rows to columns).

203806-image.png




Could you please help me with the SQL to achieve this.

sql-server-transact-sql
image.png (31.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NaomiNNN avatar image
0 Votes"
NaomiNNN answered

The simplest solution would be the regular case based pivot, e.g.

select dt, lst_yr_dt, lst2_yer_dt, region, bird,
max(case when desg = 'svp' then orid end) as svp_orid,
max(case when desg = 'vp' then orid end) as vp_orid,
etc.

from SourceData
group by dt, lst_yr_dt, lst2_yer_dt, region, bird

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.