SQL Server: Examples of PIVOTing String data

前端 未结 7 1272
忘掉有多难
忘掉有多难 2020-11-21 13:20

Trying to find some simple SQL Server PIVOT examples. Most of the examples that I have found involve counting or summing up numbers. I just want to pivot some string data.

7条回答
  •  心在旅途
    2020-11-21 13:44

    Remember that the MAX aggregate function will work on text as well as numbers. This query will only require the table to be scanned once.

    SELECT Action,
           MAX( CASE data WHEN 'View' THEN data ELSE '' END ) ViewCol, 
           MAX( CASE data WHEN 'Edit' THEN data ELSE '' END ) EditCol
     FROM t
     GROUP BY Action
    

提交回复
热议问题