Convert Rows to columns using 'Pivot' in mssql when columns are string data type

前端 未结 3 1709
情深已故
情深已故 2020-12-16 04:10

I need to know whether \'pivot\' in MS SQL can be used for converting rows to columns if there is no aggregate function to be used. i saw lot of examples with aggregate func

3条回答
  •  悲&欢浪女
    2020-12-16 04:21

    sample format

    empid     wagecode    amount
    1              basic           1000
    1              TA               500
    1              DA               500
    2              Basic           1500
    2              TA               750
    2              DA               750

    empid      basic       TA        DA
    1            1000         500      500
    2            1500         750       750

    THE ANSWER I GOT IS

       SELECT empID , [1bas] as basic, [1tasal] as TA,[1otsal] as DA
       FROM (
       SELECT empID, wage, amount
       FROM table) up
       PIVOT (SUM(amt) FOR wgcod IN ([1bas], [1tasal],[1otsal])) AS pvt
       ORDER BY empID 
       GO
    

提交回复
热议问题