2

我正在使用 SQL Server 2012,我需要帮助才能获得以下输出。我的表结构如下:

declare @TestTable table (MaterialCode char(5), ItemCode char(5), ItemName varchar(50))

insert into @TestTable values ('AA-01', 'A0001', 'iPhone')
insert into @TestTable values ('AA-02', 'A0001', 'iPad')
insert into @TestTable values ('AA-03', 'A0001', 'iPod')

insert into @TestTable values ('AA-01', 'B0001', 'Galaxy Tab')
insert into @TestTable values ('AA-02', 'B0001', 'Galaxy Note')

insert into @TestTable values ('AA-01', 'C0001', 'Nokia Lumnia')
insert into @TestTable values ('AA-02', 'C0001', 'Motorola')
insert into @TestTable values ('AA-03', 'C0001', 'Samsung S3')
insert into @TestTable values ('AA-04', 'C0001', 'Sony')

--select * from @TestTable

select MaterialCode, ItemCode as [A_ItemCode], ItemName as [A_ItemName] 
from @TestTable where ItemCode='A0001'

select MaterialCode, ItemCode as [B_ItemCode], ItemName as [B_ItemName] 
from @TestTable where ItemCode='B0001'

select MaterialCode, ItemCode as [C_ItemCode], ItemName as [C_ItemName] 
from @TestTable where ItemCode='C0001'

而我需要的输出应该是从上面三个select语句产生的,应该如下:

在此处输入图像描述

如您所见,当没有记录时,NULL 会显示在那里。谁能帮我得到这个输出。TIA。

编辑

@JohnLBevan,我尝试了您的枢轴方法,当我有另一条记录时 ItemCode = D0001

insert into @TestTable values ('AA-05', 'D0001', 'Test1')

现在,即使该记录也显示为

AA-05   NULL    NULL    NULL    NULL    NULL    NULL

如何避免这些类型的记录。

4

1 回答 1

3

不使用枢轴,但这会给你想要的结果:

SELECT coalesce(a.MaterialCode, b.MaterialCode, c.MaterialCode) MaterialCode 
, a.ItemCode A_ItemCode
, a.ItemName A_ItemName
, b.ItemCode B_ItemCode
, b.ItemName B_ItemName
, c.ItemCode C_ItemCode
, c.ItemName C_ItemName
FROM (select * from @TestTable where ItemCode='A0001') a
full outer join (select * from @TestTable where ItemCode='B0001') b
    on a.MaterialCode = b.MaterialCode
full outer join (select * from @TestTable where ItemCode='C0001') c
    on a.MaterialCode = c.MaterialCode
    or b.MaterialCode = c.MaterialCode

这是一个使用枢轴的替代方案,尽管这感觉更混乱:

SELECT MaterialCode
, case when [A0001] is null then null else 'A0001' end A_ItemCode
, [A0001] A_ItemName
, case when [A0001] is null then null else 'B0001' end B_ItemCode
, [B0001] B_ItemName
, case when [A0001] is null then null else 'C0001' end C_ItemCode
, [C0001] C_ItemName
FROM @TestTable a
pivot (
    max(ItemName) 
    for ItemCode in ([A0001],[B0001],[C0001])
) pvt

编辑

如果你想要更灵活的东西,这里有一个动态版本:

declare @dynamicSql nvarchar(max)
, @dynamicSqlPart nvarchar(max)

select * into #TestTable from @TestTable

set @dynamicSql = 'SELECT MaterialCode ' + CHAR(10)
select @dynamicSql = @dynamicSql + ', case when [' + ItemCode + '] is null then null else ''' + ItemCode + ''' end ' + ItemCode + '_ItemCode ' + CHAR(10)
    + ', ' + QUOTENAME(ItemCode) + ' ' + ItemCode + '_ItemName ' + CHAR(10)
, @dynamicSqlPart = ISNULL(@dynamicSqlPart + ',','') +  QUOTENAME(ItemCode)
from (select distinct ItemCode from @TestTable) x

set @dynamicSql = @dynamicSql + 'from #TestTable a' + CHAR(10)
    + 'pivot (' + CHAR(10)
    + ' max(ItemName) ' + CHAR(10)
    + ' for ItemCode in (' + @dynamicSqlPart + ')' + CHAR(10)
    + ') pvt' + CHAR(10)

print isnull(@dynamicSql,'?')
exec (@dynamicSql)

drop table #TestTable
于 2012-11-08T08:43:23.883 回答