行到列SQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了行到列SQL相关的知识,希望对你有一定的参考价值。

您好我正在进行查询以更改此格式:

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;
答案

您可以使用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
另一答案

试试这个:

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

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

以上是关于行到列SQL的主要内容,如果未能解决你的问题,请参考以下文章

SQL查询到行到列[重复]

动态透视行到列-SQL 服务器

sql server中的表行到列

SQL 查询中的行到列

从行到列的 SQL 结果

基于月份的 Oracle SQL 数据迁移行到列因类型而失败