2014-01-16 81 views
-1

我正在將數據從舊數據庫傳輸到新數據庫。在舊的數據庫格式永樂這樣使用sql server 2008 r2將行重新排列成列?

enter code here

我需要更改這個像這樣

enter image description here

誰能告訴我怎麼解決這個....,

+0

是條形碼和流程在所有行中唯一的組合嗎? – Segfault

+3

也許[這](http://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx)會給你一個提示.. – Nico

+0

@Nico你能簡單解釋一下 – Pallavi

回答

1

您可以使用Pivot語句來做到這一點,正如評論所暗示的那樣,但在我看來,使用連接更直接。兩次將同一個表加入自己,然後將結果集中的表從三次中選擇。如果你的進程是一致的這三個那麼我建議這樣的:

SELECT a.barcode, a.process as First, a.volume as FirstVolume, 
     b.process as Second, b.volume as Second Volume, 
     c.process as Third, c.volume as ThirdVolume 
FROM Volumes a 
LEFT JOIN Volumes b on b.barcode = a.barcode and b.process = 'BC' 
LEFT JOIN Volumes c on c.barcode = a.barcode and c.process = 'FPC' 
WHERE a.process = 'PUC'; 

這使得假定總有一個過程市局...

如果你不知道什麼樣的價值觀可能會在這個過程中柱,我們可以做這樣的:

SELECT a.barcode, a.process as First, a.volume as FirstVolume, 
     b.process as Second, b.volume as Second Volume, 
     c.process as Third, c.volume as ThirdVolume 
FROM Volumes a 
LEFT JOIN Volumes b on b.barcode = a.barcode and b.process <> a.process 
LEFT JOIN Volumes c on c.barcode = a.barcode and c.process <> a.process and c.process <> b.process; 

這不作任何保證第一進程列各行上雖然相同,只有你得到三個不同的流程,每行每一個條形碼。

+0

它工作正常......謝謝 – Pallavi

相關問題