2016-10-28 24 views
0

请帮助我建立在Microsoft SQL Server中选择SQL,我想从表中转换:转换多行与从第一表中的许多列和名称多行

enter image description here

转换为新的表,得到不同的DeviceCode和新的列从PartName值中获得,并且PartName值是无限的(DVD,鼠标,键盘,显示器......):

我只是在那里附上完整的图像,对不起,我不能在帖子中添加表格查看器:https://farm6.staticflickr.com/5628/30615995995_a0ed8b65fa_o_d.jpg

+0

可以显示其他零件类型吗?一个设备的几个相同类型的部件呢? – jarlh

+0

有一个设备的几个部分,设备可以在以后添加更多的部分:例如 - 电脑有一些部分:CPU,RAM,主板,鼠标,键盘,显示器,电源sup .....部分只有2信息:PartName和信息 –

+0

因此,添加更多示例数据,使事情更加复杂! – jarlh

回答

0
create table #d 
( 
device_id int, 
device_code varchar(10),part_id int,part_name varchar(100),info varchar(100) 
) 
insert into #d values 
(1,'1.1g',1,'ram','szaga'), 
(1,'1.1g',3,'mother boad','saagg'), 
(1,'1.1g',4,'cpu','dgsg') 


    select device_id,device_code, 
     max(case when part_name = 'ram' then info end) Firstname, 
     max(case when part_name = 'mother boad' then info end) Amount, 
     max(case when part_name = 'cpu' then info end) PostalCode 
    from #d 
    group by device_id,device_code 
+0

非常感谢您的答案,我会尝试它! –

+0

是否满足您的要求.. @ HienNguyen – Chanukya