我有一个表叫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