假設你不需要去動態,與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
驗證結果
你嘗試過這麼遠嗎? – fen1x
我用這樣strSql1 =「select distinct TrNo,ManRef,Transport from mytable – axna2000