SQL Pivot返回相同的值

时间:2017-12-11 04:51:41

标签: sql sql-server tsql pivot

我正试图在桌子上运行一个如下所示的轴。

Item    Barcode Primary
Test1   111111  Y
Test1   222222  N
Test1   333333  N
Test1   444444  N
Test2   999999  Y
Test2   888888  N
Test2   777777  N
Test2   666666  N

我正在尝试创建一个看起来像这样的表。 (每列一个条形码)

Item    Primary Sec1    Sec2    Sec3  
Test1   111111  222222  333333  444444
Test2   999999  888888  777777  666666

使用以下查询,我可以填充主列和第一个辅助列,但是我找不到将其他辅助代码放到自己的列中的方法。相反,它只是重复第一个二级条形码,如下所示。

Item    Primary Barcode Secondary 1 Secondary3
Test1   111111  222222  222222
Test2   999999  888888  888888

select item, [Y] as 'Primary', [N] as 'Sec1', [N] as 'Sec2'
from
(
    select ics.itemcoloursize_id as item,  sc.sellcode_code as code, sc.primary_ind as ind
    from sellcode sc
    left join itemcoloursize ics on ics.itemcoloursize_id = sc.itemcoloursize_id
    where ics.itemcoloursize_id in 
    (
    's+0015p00fhb'
    )  
)   as Barcodes
PIVOT
(
    max(code)
    for ind in ([Y],[N])
    )
    as PVT

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

如果您不想使用case或某些动态方法,可以在pivot表达式的帮助下尝试条件聚合

select Item,
      max(case when [Primary] = 'Y' then Barcode else null end) [Primary],
      max(case when ([Primary] = 'N' AND [RN] = 1) then Barcode else null end) [Sec1],
      max(case when ([Primary] = 'N' AND [RN] = 2) then Barcode else null end) [Sec2],
      max(case when ([Primary] = 'N' AND [RN] = 3) then Barcode else null end) [Sec3]  
      from 
(
    select *, row_number() over (partition by Item, [Primary] order by Item) [rn] from table
) t
group by Item

结果:

Item    Primary Sec1    Sec2    Sec3
Test1   111111  222222  333333  444444
Test2   999999  888888  777777  666666

答案 1 :(得分:0)

试试这个并可能对您有所帮助。

解决方案:
 通过点分隔符逐项组合条形码值 然后使用parsename函数将条形码作为单个列分隔

    select  item,parsename(commasep,4)Primry,parsename(commasep,3)sec1, 
    parsename(commasep,2)sec2,parsename(commasep,1)sec3
    from
     (select item,stuff((select '.' + u.barcode from pivottest u
     where u.item = p.item order by u.isprimary desc
     for xml path('')),1,1,'') as commasep
     from pivottest p group by item) as parsecommasep

结果:

item Primry sec1 sec2 sec3 Test 1 111111 222222 333333 444444 Test 2 999999 888888 777777 666666

答案 2 :(得分:0)

尝试使用此脚本,并了解其他样本数据是否有任何错误或输出错误

同样建议使用sp_executesql

CREATE TABLE #t (Item VARCHAR(50)
,Barcode VARCHAR(50),Primarys CHAR(1))

INSERT INTO #t VALUES   
 ('Test1','111111','Y')
,('Test1','222222','N')
,('Test1','333333','N')
,('Test1','444444','N')
,('Test2','999999','Y')
,('Test2','888888','N')
,('Test2','777777','N')
--,('Test2','666666','N')

--SELECT * FROM #t

DECLARE @ColValue VARCHAR(500)
DECLARE @ColName VARCHAR(500)

;WITH CTE
AS (
    SELECT Item
        ,Barcode
        ,Primarys
        ,ROW_NUMBER() OVER (
            PARTITION BY item ORDER BY item
            ) rn
    FROM #t
    )
    ,CTE1
AS (
    SELECT Item
        ,Barcode
        ,Primarys
        ,rn
        ,CASE 
            WHEN Primarys = 'Y'
                THEN 'Primary'
            ELSE 'Sec' + cast(rn - 1 AS VARCHAR)
            END Headers
    FROM cte
    )
SELECT *
INTO #tt
FROM cte1


SELECT @ColName = stuff((
            SELECT ',[' + Headers + ']'
            FROM #tt t1
            WHERE t.Item = t1.Item
            FOR XML PATH('')
            ), 1, 1, '')
FROM #tt t
ORDER BY rn

--SELECT @ColName
DECLARE @Sql VARCHAR(500) = ''

SET @Sql = '
select item,' + @ColName + ' from 
(
select item,Barcode,Headers from #tt
)base
pivot(max(Barcode) for Headers in(' + @ColName + ')) as pvt
'

PRINT @sql

EXEC (@Sql)

DROP TABLE #tt

DROP TABLE #t

这里#t是你的table.leave #tt原样。它可以根据其他事情进一步优化。