我希望有一個case語句(或您建議的)生成多個結果,並且這些多個結果會反映在輸出中。但是,使用底部的示例,我沒有得到期望的結果。提前致謝。T-SQL:將列值移動到行值
目前:
ID Phase Total Hours Team1 Team2 Team3
1 Test 50 25 10 15
2 QA 60 20 20 20
3 Impl 40 0 20 20
尋找:
ID Phase Total Hours Team Name Team Hour
1 Test 50 Team 1 25
1 Test 50 Team 2 10
1 Test 50 Team 3 15
2 QA 60 Team 1 20
2 QA 60 Team 2 20
2 QA 60 Team 3 20
3 Impl 40 Team 2 20
3 Impl 40 Team 3 20
Select ID, Phase, Total Hours,
case
When Team1 is not null and Team1 is >0 then 'Team1'
When Team2 is not null and Team2 is >0 then 'Team2'
When Team3 is not null and Team3 is >0 then 'Team3'
end as 'Team Name',
case
When Team1.Hrs is not null and Team1.Hrs is >0 then Team1.Hrs
When Team2.Hrs is not null and Team2.Hrs is >0 then Team2.Hrs
When Team3.Hrs is not null and Team3.Hrs is >0 then Team3.Hrs
end as 'Team Hours'
From DB.DBNAME
您的示例查詢產生了什麼? – JNK