TSQL Pivot without aggregate function

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

I have a table like this...

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


        
相关标签:
9条回答
  • 2020-11-22 09:52
    SELECT
    main.CustomerID,
    f.Data AS FirstName,
    m.Data AS MiddleName,
    l.Data AS LastName,
    d.Data AS Date
    FROM table main
    INNER JOIN table f on f.CustomerID = main.CustomerID
    INNER JOIN table m on m.CustomerID = main.CustomerID
    INNER JOIN table l on l.CustomerID = main.CustomerID
    INNER JOIN table d on d.CustomerID = main.CustomerID
    WHERE f.DBColumnName = 'FirstName' 
    AND m.DBColumnName = 'MiddleName' 
    AND l.DBColumnName = 'LastName' 
    AND d.DBColumnName = 'Date' 
    

    Edit: I have written this without an editor & have not run the SQL. I hope, you get the idea.

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

    The OP didn't actually need to pivot without agregation but for those of you coming here to know how see:

    sql parameterised cte query

    The answer to that question involves a situation where pivot without aggregation is needed so an example of doing it is part of the solution.

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

    This should work:

    select * from (select [CustomerID]  ,[Demographic] ,[Data]
    from [dbo].[pivot]
    ) as Ter
    
    pivot (max(Data) for  Demographic in (FirstName, MiddleName, LastName, [Date]))as bro
    
    0 讨论(0)
提交回复
热议问题