Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回相同值的SQL Pivot_Sql_Sql Server_Tsql_Pivot - Fatal编程技术网

返回相同值的SQL Pivot

返回相同值的SQL Pivot,sql,sql-server,tsql,pivot,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 Barcode Secondary 1 Secondary3 Test1 11

我试着在一张桌子上运行一个轴,它看起来像下面的

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 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
我正在尝试创建一个如下所示的表。(每列一个条形码)

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

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 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

任何帮助都将不胜感激。

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

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

试试这个,可能对你有帮助

解决方案:
按点分隔符按项目分组组合条形码值
然后使用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 primary sec1 sec2 sec3
测试1 111111 22222 333 333 444444

测试2 999999 8888 777777 666666尝试此脚本,让我知道是否有任何错误或输出与其他样本数据不正确

此外,建议使用
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

这是您的表。保持原样。它可以根据其他内容进行进一步优化。

您如何知道22222应该是Sec1,而不是Sec2?是否有其他列对行进行排序?任何带有primary_ind='N'的条形码都应作为Sec1-x显示在其自己的列中。这些建议的顺序无关紧要。我已经尝试了以上两种建议。他们俩都不能为我工作。任何其他帮助都将不胜感激。