LaysomeSmith
LaysomeSmith

Reputation: 681

Query on query result

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

Answers (2)

Taryn
Taryn

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

Chandu
Chandu

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

Related Questions