Pivot rows to columns for multiple columns

Suman Gupta 61 Reputation points
2022-05-19T18:08:45.303+00:00

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.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Naomi 7,361 Reputation points
    2022-05-19T18:22:15.75+00:00

    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

    0 comments No comments

0 additional answers

Sort by: Most helpful