2016-10-28 60 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