SQL Server: Examples of PIVOTing String data

前端 未结 7 1259
忘掉有多难
忘掉有多难 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:40
    With pivot_data as
    (
    select 
    action, -- grouping column
    view_edit -- spreading column
    from tbl
    )
    select action, [view], [edit]
    from   pivot_data
    pivot  ( max(view_edit) for view_edit in ([view], [edit]) ) as p;
    
    0 讨论(0)
  • 2020-11-21 13:41

    Well, for your sample and any with a limited number of unique columns, this should do it.

    select 
        distinct a,
        (select distinct t2.b  from t t2  where t1.a=t2.a and t2.b='VIEW'),
        (select distinct t2.b from t t2  where t1.a=t2.a and t2.b='EDIT')
    from t t1
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-21 13:49

    From http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/:

    SELECT CUST, PRODUCT, QTY
    FROM Product) up
    PIVOT
    ( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt) p
    UNPIVOT
    (QTY FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)
    ) AS Unpvt
    GO
    
    0 讨论(0)
  • 2020-11-21 13:53

    Table setup:

    CREATE TABLE dbo.tbl (
        action VARCHAR(20) NOT NULL,
        view_edit VARCHAR(20) NOT NULL
    );
    
    INSERT INTO dbo.tbl (action, view_edit)
    VALUES ('Action1', 'VIEW'),
           ('Action1', 'EDIT'),
           ('Action2', 'VIEW'),
           ('Action3', 'VIEW'),
           ('Action3', 'EDIT');
    

    Your table: SELECT action, view_edit FROM dbo.tbl

    Your table

    Query without using PIVOT:

    SELECT Action, 
    [View] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'VIEW'),
    [Edit] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'EDIT')
    FROM tbl t
    GROUP BY Action
    

    Query using PIVOT:

    SELECT [Action], [View], [Edit] FROM
    (SELECT [Action], view_edit FROM tbl) AS t1 
    PIVOT (MAX(view_edit) FOR view_edit IN ([View], [Edit]) ) AS t2
    

    Both queries result:
    enter image description here

    0 讨论(0)
  • 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])
     )
    
    0 讨论(0)
提交回复
热议问题