行到列SQL

时间:2017-12-20 21:07:30

标签: sql sql-server pivot

您好我正在处理一个更改此格式的查询:

enter image description here

到此:

enter image description here

我一直在使用此代码,但不确定我是否遵循了正确的方法:

select [Duplicate Stores],[Missing Products],channel,Date
from (SELECT
    CheckType,
    Channel,
    Date,
    AttributeValue
FROM MissingList 
 ) d
pivot (
 max(AttributeValue)
 for CheckType in ([Duplicate Stores], [Missing Products])
) piv;

2 个答案:

答案 0 :(得分:2)

您可以使用STUFFFOR XML来获取逗号分隔的属性值,如下所示:

SELECT
    [Duplicate Stores]
    ,[Missing Products]
    ,channel
    ,[Date]
FROM (
         SELECT
             m2.CheckType
             ,m2.Channel
             ,m2.[Date]
             ,Attribute = STUFF(
                          (
                              SELECT ',' + CAST(m1.Attribute AS VARCHAR)
                              FROM MissingList m1
                              WHERE m2.CheckType = m1.CheckType
                                  AND m2.Channel = m1.Channel
                                  AND m2.[Date] = m1.[Date] 
                              FOR XML PATH('')
                          )    , 1, 1, ''
                               )
         FROM MissingList m2
         GROUP BY m2.CheckType
             ,m2.Channel
             ,m2.[Date]
     ) d
PIVOT (   MAX(Attribute)
          FOR CheckType IN ([Duplicate Stores], [Missing Products])
      ) piv;

编辑: 以下是我使用的示例数据,基于OP的问题:

CREATE TABLE #Missinglist (CheckType VARCHAR(100), Channel VARCHAR(10), [Date] DATE, Attribute INT)

INSERT INTO #Missinglist (CheckType, Channel, Date, Attribute)
VALUES ('Duplicate Stores','WMT','12/20/2017',4055),
('Duplicate Stores','WMT','12/20/2017',6807),
('Duplicate Stores','WMT','12/20/2017',7020),
('Missing Products','WMT','12/20/2017',3484046),
('Missing Products','WMT','12/20/2017',3219002),
('Missing Products','WMT','12/20/2017',5875045),
('Duplicate Stores','BB','1/1/2017',243424),
('Duplicate Stores','BB','1/1/2017',24234),
('Duplicate Stores','BB','1/1/2017',66767),
('Missing Products','BB','1/1/2017',8895),
('Missing Products','BB','1/1/2017',236),
('Missing Products','BB','1/1/2017',7356),
('Duplicate Stores','BB','1/2/2017',234),
('Duplicate Stores','BB','1/2/2017',75237),
('Duplicate Stores','BB','1/2/2017',232),
('Missing Products','BB','1/2/2017',883),
('Missing Products','BB','1/2/2017',236),
('Missing Products','BB','1/2/2017',7356)

使用我的查询产生以下结果集:

Duplicate Stores    Missing Products        channel Date
--------------------------------------------------------------
243424,24234,66767  8895,236,7356           BB      2017-01-01
234,75237,232       883,236,7356            BB      2017-01-02
4055,6807,7020      3484046,3219002,5875045 WMT     2017-12-20

答案 1 :(得分:0)

试试这个:

with #all as 
(select * from YourTable)
 select * from (select Date, Channel,
,case when Checktype ='Duplicated Stores' then 'Duplicated Stores'
    when Checktype ='Missing Products' then 'Missing Products'
    end as seqno
from #all
) as datatable
pivot(
Max([ATTRIBUTE]) 
for [seqno] in ([Duplicated Stores],[Missing Products])
)as piv

然而,这不会对每个属性进行分组,并以逗号为单位使用它们。相反,它会单独为您提供每个属性。