我有像这样的结果集结构
ID Value Name
1 Oranges Reponse
1 42 Count
2 Apples Reponse
2 65 Count
3 Figs Reponse
3 74 Count
我想谈谈这个:
ID Response Count
1 Oranges 42
2 Apples 65
3 Figs 74
使用SQL。有没有办法做到这一点?谢谢!
答案 0 :(得分:13)
SELECT a.ID, a.Value AS [Response], b.Value AS [Count]
FROM your_table AS a
INNER JOIN your_table AS b
ON a.ID = b.ID
WHERE a.Name = 'Response'
AND b.Name = 'Count'
答案 1 :(得分:6)
这始终是一个非常繁琐的pre sql server 2005。
现在我使用PIVOT/UNPIVOT
答案 2 :(得分:0)
SELECT A.ID, A.VALUE RESPONSE, C.VALUE COUNT
FROM _table A
INNER JOIN (
SELECT ID, VALUE, NAME
FROM _table
WHERE _table.Name = 'Count'
) C ON A.ID = C.ID
WHERE A.NAME='Response' and C.NAME='Count'