2017-08-10 153 views
1
I have data in my table like this 

TrNo | ManRef | Transport 
001 ASC1  30000 
001 ASC1  15000 
002 ASC2  30000 

,我想成爲這樣的拆分數據

TrNo | ManRef | Transport 1 | Transport 2 
001 ASC1  30000   15000 
002 ASC2  30000   - 

so..any一個可以幫助我,如何查詢該表的數據... pleaseee

+0

你嘗試過這麼遠嗎? – fen1x

+0

我用這樣strSql1 =「select distinct TrNo,ManRef,Transport from mytable – axna2000

回答

1

假設你不需要去動態,與ROW_NUMBER(演唱會簡單的條件聚合)應該做的伎倆

Select TrNo 
     ,ManRef 
     ,sum(case when RN=1 then Transport else 0 end) as Transport1 
     ,sum(case when RN=2 then Transport else 0 end) as Transport2 
     ,sum(case when RN=3 then Transport else 0 end) as Transport3 
     ,sum(case when RN=4 then Transport else 0 end) as Transport4 
     ,sum(case when RN=5 then Transport else 0 end) as Transport5 
     ,sum(case when RN=6 then Transport else 0 end) as Transport6 
From (
     Select * 
       ,Row_Number() over (Partition By TrNo,ManRef Order By Transport Desc) as RN 
     From YourTable 
    ) A 
Group By TrNo,ManRef 

驗證結果

enter image description here

+0

jhon代碼不工作任何建議 – axna2000

+0

@ axna2000它是非常通用的。什麼數據庫你inor什麼錯誤正在返回 –

+0

語法錯誤(缺少運算符)in查詢表達式'sum(case RN = 1 then then else 0 end)'我用vb6 – axna2000