jung1975
asked on
subquery in pivot function sql server 2005
I would like to pivot the columns comes from the sub-query using pivot function in sql 2005...Like a below example, I would like to pivot cities returned by a sub-query : select cities from reasion a join contry b on a.region_id = b.region_id where region_cd = 99
Is there anyway I can use a sub-query in pivot function? if i have to use a dynamic sql to return the sub-query value in pivot query, can you show me some code examples?
Select
consume_ID,
(select cities from reasion a join contry b on a.region_id = b.region_id where region_cd = 99 )
FROM
(SELECT
SELECT Consumer_ID , Visit_value,city_test
FROM person p INNER JOIN Inventory I on p.inventory_id = I.inventory_id)
WHERE (Visits_ID = 4)
) p
PIVOT
(
count(visit_Value) For city IN
( select cities from reasion a join contry b on a.region_id = b.region_id where region_cd = 99 )
) pvt;
Is there anyway I can use a sub-query in pivot function? if i have to use a dynamic sql to return the sub-query value in pivot query, can you show me some code examples?
Select
consume_ID,
(select cities from reasion a join contry b on a.region_id = b.region_id where region_cd = 99 )
FROM
(SELECT
SELECT Consumer_ID , Visit_value,city_test
FROM person p INNER JOIN Inventory I on p.inventory_id = I.inventory_id)
WHERE (Visits_ID = 4)
) p
PIVOT
(
count(visit_Value) For city IN
( select cities from reasion a join contry b on a.region_id = b.region_id where region_cd = 99 )
) pvt;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.