TSQL Pivot without aggregate function

后端 未结 9 1752
青春惊慌失措
青春惊慌失措 2020-11-22 09:10

I have a table like this...

CustomerID   DBColumnName   Data
--------------------------------------
1            FirstName      Joe
1            MiddleName           


        
相关标签:
9条回答
  • 2020-11-22 09:35
    WITH pivot_data AS
    (
    SELECT customerid, -- Grouping Column
    dbcolumnname, -- Spreading Column
    data -- Aggregate Column
    FROM pivot2 
    )
    SELECT customerid, [firstname], [middlename], [lastname]
    FROM pivot_data
    PIVOT (max(data) FOR dbcolumnname IN ([firstname],[middlename],[lastname])) AS p;
    
    0 讨论(0)
  • 2020-11-22 09:37

    Here is a great way to build dynamic fields for a pivot query:

    --summarize values to a tmp table

    declare @STR varchar(1000)
    SELECT  @STr =  COALESCE(@STr +', ', '') 
    + QUOTENAME(DateRange) 
    from (select distinct DateRange, ID from ##pivot)d order by ID
    

    ---see the fields generated

    print @STr
    
    exec('  .... pivot code ...
    pivot (avg(SalesAmt) for DateRange IN (' + @Str +')) AS P
    order by Decile')
    
    0 讨论(0)
  • 2020-11-22 09:43

    yes, but why !!??

       Select CustomerID,
         Min(Case DBColumnName When 'FirstName' Then Data End) FirstName,
         Min(Case DBColumnName When 'MiddleName' Then Data End) MiddleName,
         Min(Case DBColumnName When 'LastName' Then Data End) LastName,
         Min(Case DBColumnName When 'Date' Then Data End) Date
       From table
       Group By CustomerId
    
    0 讨论(0)
  • 2020-11-22 09:45

    Try this:

    SELECT CUSTOMER_ID, MAX(FIRSTNAME) AS FIRSTNAME, MAX(LASTNAME) AS LASTNAME ...
    
    FROM
    (
    
    SELECT CUSTOMER_ID, 
           CASE WHEN DBCOLUMNNAME='FirstName' then DATA ELSE NULL END AS FIRSTNAME,
           CASE WHEN DBCOLUMNNAME='LastName' then DATA ELSE NULL END AS LASTNAME,
            ... and so on ...
    GROUP BY CUSTOMER_ID
    
    ) TEMP
    
    GROUP BY CUSTOMER_ID
    
    0 讨论(0)
  • 2020-11-22 09:46

    Ok, sorry for the poor question. gbn got me on the right track. This is what I was looking for in an answer.

    SELECT [FirstName], [MiddleName], [LastName], [Date] 
    FROM #temp 
    PIVOT
    (   MIN([Data]) 
        FOR [DBColumnName] IN ([FirstName], [MiddleName], [LastName], [Date]) 
    )AS p
    

    Then I had to use a while statement and build the above statement as a varchar and use dynmaic sql.

    Using something like this

    SET @fullsql = @fullsql + 'SELECT ' + REPLACE(REPLACE(@fulltext,'(',''),')','')
    SET @fullsql = @fullsql + 'FROM #temp '
    SET @fullsql = @fullsql + 'PIVOT'
    SET @fullsql = @fullsql + '('
    SET @fullsql = @fullsql + ' MIN([Data])'
    SET @fullsql = @fullsql + ' FOR [DBColumnName] IN '+@fulltext
    SET @fullsql = @fullsql + ')'
    SET @fullsql = @fullsql + 'AS p'
    
    EXEC (@fullsql)
    

    Having a to build @fulltext using a while loop and select the distinct column names out of the table. Thanks for the answers.

    0 讨论(0)
  • 2020-11-22 09:50

    You can use the MAX aggregate, it would still work. MAX of one value = that value..

    In this case, you could also self join 5 times on customerid, filter by dbColumnName per table reference. It may work out better.

    0 讨论(0)
提交回复
热议问题