SQL Pivot help

  • How would I pivot this query I need uompschedulenumber to be the ROW, QTY to be the summary data and redtagcode to be the columns([MIL],[CON] ,[DAM],[FIN],[NON] ,[CLM],[REP],[RRT],[SHT])

    select uompScheduleNumber,

    case

    when redtagcode ='' then 'NON' else redtagcode end as code

    ,count(*) as qty

    from wip_master where redtag='Y' and createdate >=DATEADD(month, DATEDIFF(month, 0, getdate() ), 0)

    group by uompScheduleNumber,redtagcode

    ORDER BY uompScheduleNumber,redtagcode

    I add and attachment with sample data

  • Please post a CREATE TABLE statement for the table, post a few handful rows of sample data as INSERT statements, and post the exact results you want returned from that data.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hi,

    This query may be helpfull

    select * from

    (

    select * from wip_master

    ) p

    pivot

    (

    sum(qty) for code in ([CLM],[CON],[DAM],[FIN],[MIL],[NON],[REP],[RRT],[SHT])

    ) as result

    --chalam

  • That worked I was able to add my where clause to it and get what I need thanks

    select * from

    (

    select uompScheduleNumber, case

    when redtagcode ='' then 'NON' else redtagcode end as code

    ,count(*) as qty from wip_master where redtag='Y' and createdate >='02-23-2016' group by uompScheduleNumber,redtagcode

    ) p

    pivot

    (

    sum(qty) for code in ([CLM],[CON],[DAM],[FIN],[MIL],[NON],[REP],[RRT],[SHT])

    ) as result

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply