Reputation: 681
http://sqlfiddle.com/#!3/e4891/1
for the above resulting query(which pivots data) I want to run my one more select condition.
For example
select (C1 & C2) from ‘the above pivoted query results’
IS it possible to do it? CTE will help here? SQL Server 2008 and above.
Upvotes: 1
Views: 53
Reputation: 247620
either change your select to only select the fields that you want:
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
Upvotes: 0
Reputation: 82893
Try this:
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
Upvotes: 1