我有一個表叫Col_values和Col_ID是一個標識字段中的數據是這樣的:
Val_ID Col_ID Value
76951792 3 Closed
76951791 3 Closed
76951790 25 Open
76951789 25 Closed
76951792 1 US
76951791 1 Canada
和另一個表稱爲Et_Col,數據如下所示:
Col_ID Col_Name D_ID
3 Status 1
25 Status 2
1 Country 1
我想要的輸出:
Val_ID Status Country
76951792 Closed US
76951791 Closed Canada
76951790 Open Null
76951789 Closed Null
所以我寫了一個光標,以獲得輸出光標返回語句,如:
Select val_ID
,max(case when Col_ID = 3 then Value end) as Status
,max(case when Col_ID = 25 then Value end) as Status
,max(CASE WHEN Col_ID = 1 THEN VALUE END) AS Country
From Col_values
Group by Val_ID
這給了我輸出:
Val_ID Status Status Country
76951792 Closed Null US
76951791 Closed Null Canada
76951789 Null Closed Null
76951790 Null Open Null
我該如何解決這個問題?
你知道術語標準化嗎? – 2011-12-19 17:51:41
這可以通過動態數據透視表完成:http://stackoverflow.com/questions/8327261/dynamic-sql-pivot-in-sql-server – JeffO 2011-12-19 18:07:52