How Do I Use PIVOT On This Data:?

前端 未结 4 1270
抹茶落季
抹茶落季 2021-01-16 16:24

I have a SQL Server table that looks like this:

RESOURCE |  DESCRIPTION | VALUE
Test A      Name        | Resource A-xyz
Test A   |  Height      | 20
Test A          


        
相关标签:
4条回答
  • 2021-01-16 16:42

    In the end, I did the following:

    1. Selected all distinct descriptions (more than 70!).
    2. Created a table that had resource and every single distinct description as fields
    3. Populated resource column distinct resource names
    4. Ran a series of updates to populate remaining columns for each distinct resource name.

    For example

    CREATE TABLE #tb1
    (
         [RESOURCE] varchar(100),
         [FIELD1]   varchar(100),
         [FIELD2]   varchar(50),
         .
         .
         .
         [LAST FIELD]  varchar(50),
    )
    
    INSERT INTO #tb1 (RESOURCE)
    SELECT DISTINCT RESOURCE FROM tb2
    ORDER BY Resource ASC
    
    UPDATE #tb1 SET [FIELD1] = (SELECT VALUE FROM tb2 WHERE Resource = #tb1.Resource and  Property = [FIELD1])
    .
    .
    .
    UPDATE #tb1 SET [LAST FIELD] = (SELECT VALUE FROM tb2 WHERE Resource = #tb1.Resource and  Property = [LAST FIELD])
    
    0 讨论(0)
  • 2021-01-16 16:43

    If you don't want to hardcode the columns and want to generate same view on the fly, you can use this. This will generate dynamic pivot

    CREATE TABLE demo
    (
    RESOURCE VARCHAR(100),
    DESCRIPTION VARCHAR(100), VALUE VARCHAR(100)
    )
    
    INSERT INTO demo VALUES
    ('Test A'   ,  'Name'       , 'Resource A-xyz')
    ,('Test A'   ,  'Height'      , '20')
    ,('Test A'   ,  'Unit'     , 'ft')
    ,('Test A'   ,  'Location'   , 'Site 1')
    ,('Test B'   ,  'Volume'     , '30')
    ,('Test C'   ,  'Width'       , '10')
    ,('Test C'   ,  'Unit'        , 'in')
    
    
    SELECT DISTINCT DESCRIPTION  INTO #tbl FROM demo
    //Get list of values to be pivoted
    DECLARE @var NVARCHAR(1000)=''
    SELECT @var = @var +', ' + DESCRIPTION FROM #tbl 
    SELECT @var = SUBSTRING(@var, 2, LEN(@var))
    SELECT @var
    
    DECLARE @query NVARCHAR(2000) = 'SELECT * FROM demo PIVOT(MAX(VALUE) FOR DESCRIPTION IN ('+ @var + '))p'
    EXEC sp_executesql @query
    
    0 讨论(0)
  • 2021-01-16 16:46

    The resulting column list is based on the ordering you provide within the PIVOT:

    SELECT *
    FROM Table1
    PIVOT(MAX(VALUE) FOR DESCRIPTION IN (Name,Height,Unit,Location,Volume,Width))p
    

    Demo: SQL Fiddle

    If you have changing values for DESCRIPTION it's worthwhile to build this query dynamically, there are plenty of good examples for 'dynamic pivot' to be found.

    0 讨论(0)
  • 2021-01-16 16:59

    try this code:

    SELECT resource,Name,Height,Unit,Location,Volume,Width
    FROM 
    #T1 AS SourceTable
    PIVOT    
    (    
    max(value)    
    FOR description IN ([Name],[Height],[Unit],[Location],[Volume],[Width])    
    ) AS PivotTable 
    ORDER BY 1
    
    0 讨论(0)
提交回复
热议问题