http://sqlfiddle.com/#!3/e4891/1
对于上面得到的查询(它转动数据)我想再运行一个选择条件。
例如
从“上述轮回查询结果”
中选择(C1& C2)是否可以这样做? CTE会在这里帮忙吗? SQL Server 2008及更高版本。
答案 0 :(得分:1)
试试这个:
WITH Data AS
(
select *
from
(
select MemId, Condition_id, condition_result
from t
) x
pivot
(
sum(condition_result)
for condition_id in ([C1], [C2], [C3], [C4])
) p
)
SELECT C1, C2
FROM Data
SQLFiddle:http://sqlfiddle.com/#!3/e4891/3
答案 1 :(得分:0)
将您的选择更改为仅选择所需的字段:
select [C1], [C2]
from
(
select MemId, Condition_id, condition_result
from t
) x
pivot
(
sum(condition_result)
for condition_id in ([C1], [C2], [C3], [C4])
) p
OR
;with cte
as
(
select *
from
(
select MemId, Condition_id, condition_result
from t
) x
pivot
(
sum(condition_result)
for condition_id in ([C1], [C2], [C3], [C4])
) p
)
select [C1], [C2]
from cte