如何在2列上使用pivot函数

时间:2017-06-23 18:22:44

标签: sql sql-server-2012 pivot-table

我试图了解如何使用SQL Server的PIVOT功能来旋转2列。我见过的大多数示例都使用了超过2列,而且我不确定如何使用较少的列。

任何帮助都将受到赞赏。

列是shopper_id,而sku,我需要将shopper_id放在一列中,将左边的sku用逗号分隔。

以下是我需要转动的查询:

    SELECT
      [a].[shopper_id]
    , [a].[sku]
    FROM
     ( SELECT
         [s].[shopper_id]
       , [si].[sku]
       FROM
         [dbo].[sales] [s]
       INNER JOIN [dbo].[sales_item] [si]
       ON
         [si].[order_id] = [s].[order_id]
       WHERE
         [si].[sku] IS NOT NULL
         AND [si].[sku] NOT LIKE ''
       ORDER BY
         [si].[sku]
     ) [a];

以下是一些数据:

0ZF57CFXCBTF4G2X68B9DKH1C9DNUN37    BKAL00-30013
72YHJYU0QFMSNC61RG42TGTD53FDUM37    BKAL00-30013
4U3H3V4LX193NLY4C0FXJJD2SSS3BT37    BKAL00-30013
7UQBXPWB0CSXNYYNB1R360BJHKCQ6F47    BKAL00-30013
6WTCMTS6551PNGQRKDZRZ6NLRMB011P6    BKAL00-30013
784HKB96M5SN4VBU18U0Y3WH037F2147    BKAL0011674X
784HKB96M5SN4VBU18U0Y3WH037F2147    BKAL0011675X
TDK8YDJQMTHL48GNGVXGS6RBUCHPKK37    BKAL0020413X
R472477HL8C8N2GBP9TG6F1K7N5JV1P6    BKAL0024498
ZSM7F5FNYBXDNZU3KZTV71PY58K8KQ17    BKAL0024498
YPF1NDD5Z35V4GGMXXPCWRUF56NTKYB6    BKAL0024498
CGNYSNFF0M0WNLU2FW6NRKJ7JQL6MBW5    BKAL0025397
Y7J85S5M29LG45Y69H2QDJFX5JB17D47    BKAL0025397

预期输出应为:

shopper id                          sku1      sku2      sku3     so on...
00000148-4CDD-46C1-BD86-DF001A      MN0042258 MN0073222 MN0044842
so on...

非常感谢。

1 个答案:

答案 0 :(得分:0)

你说你想要一个逗号分隔列表。你会这样做:

declare @table table (shopper_id nvarchar(256), sku nvarchar(256))
insert into @table
values
('0ZF57CFXCBTF4G2X68B9DKH1C9DNUN37','BKAL00-30013'),
('72YHJYU0QFMSNC61RG42TGTD53FDUM37','BKAL00-30013'),
('4U3H3V4LX193NLY4C0FXJJD2SSS3BT37','BKAL00-30013'),
('7UQBXPWB0CSXNYYNB1R360BJHKCQ6F47','BKAL00-30013'),
('6WTCMTS6551PNGQRKDZRZ6NLRMB011P6','BKAL00-30013'),
('784HKB96M5SN4VBU18U0Y3WH037F2147','BKAL0011674X'),
('784HKB96M5SN4VBU18U0Y3WH037F2147','BKAL0011675X'),
('TDK8YDJQMTHL48GNGVXGS6RBUCHPKK37','BKAL0020413X'),
('R472477HL8C8N2GBP9TG6F1K7N5JV1P6','BKAL0024498'),
('ZSM7F5FNYBXDNZU3KZTV71PY58K8KQ17','BKAL0024498'),
('YPF1NDD5Z35V4GGMXXPCWRUF56NTKYB6','BKAL0024498'),
('CGNYSNFF0M0WNLU2FW6NRKJ7JQL6MBW5','BKAL0025397'),
('Y7J85S5M29LG45Y69H2QDJFX5JB17D47','BKAL0025397')

select distinct
    shopper_id,
    skuList = STUFF((
                      SELECT ',' + md.sku
                      FROM @table md
                      WHERE m.shopper_id = md.shopper_id
                      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from
    @table m

编辑基于您的上一次修改

if object_id('tempdb..#Table') is not null drop table #Table

create table #Table (shopper_id nvarchar(256), sku nvarchar(256))
insert into #Table
values
('0ZF57CFXCBTF4G2X68B9DKH1C9DNUN37','BKAL00-30013'),
('72YHJYU0QFMSNC61RG42TGTD53FDUM37','BKAL00-30013'),
('4U3H3V4LX193NLY4C0FXJJD2SSS3BT37','BKAL00-30013'),
('7UQBXPWB0CSXNYYNB1R360BJHKCQ6F47','BKAL00-30013'),
('6WTCMTS6551PNGQRKDZRZ6NLRMB011P6','BKAL00-30013'),
('784HKB96M5SN4VBU18U0Y3WH037F2147','BKAL0011674X'),
('784HKB96M5SN4VBU18U0Y3WH037F2147','BKAL0011675X'),
('TDK8YDJQMTHL48GNGVXGS6RBUCHPKK37','BKAL0020413X'),
('R472477HL8C8N2GBP9TG6F1K7N5JV1P6','BKAL0024498'),
('ZSM7F5FNYBXDNZU3KZTV71PY58K8KQ17','BKAL0024498'),
('YPF1NDD5Z35V4GGMXXPCWRUF56NTKYB6','BKAL0024498'),
('CGNYSNFF0M0WNLU2FW6NRKJ7JQL6MBW5','BKAL0025397'),
('Y7J85S5M29LG45Y69H2QDJFX5JB17D47','BKAL0025397')


if object_id('tempdb..#StagingTable') is not null drop table #StagingTable
select 
    * 
    ,'sku'+ cast(row_number() over (partition by shopper_id order by (select null)) as varchar(64)) as RN
into #StagingTable
from #Table


DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(RN)
FROM (SELECT DISTINCT RN FROM #StagingTable) AS Cname

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT shopper_id, ' + @ColumnName + '
    FROM #StagingTable
    PIVOT(min(sku) 
          FOR RN IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery