SQL Server: Examples of PIVOTing String data

前端 未结 7 1260
忘掉有多难
忘掉有多难 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 14:00

    I had a situation where I was parsing strings and the first two positions of the string in question would be the field names of a healthcare claims coding standard. So I would strip out the strings and get values for F4, UR and UQ or whatnot. This was great on one record or a few records for one user. But when I wanted to see hundreds of records and the values for all usersz it needed to be a PIVOT. This was wonderful especially for exporting lots of records to excel. The specific reporting request I had received was "every time someone submitted a claim for Benadryl, what value did they submit in fields F4, UR, and UQ. I had an OUTER APPLY that created the ColTitle and the value fields below

    PIVOT(
      min(value)
      FOR ColTitle in([F4], [UR], [UQ])
     )
    

提交回复
热议问题