I have a view vw_Country
which returns the following records:
I want to write a query using this view to return following results:
Any help would be appreciated!
I have a view vw_Country
which returns the following records:
I want to write a query using this view to return following results:
Any help would be appreciated!
Without a column to pivot on, it isn't possible, but you can create a column to pivot on. Something like this should probably work:
SELECT Rownum, [Australia], [India], [United States]
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY Country, ORDER BY City) AS Rownum, Country, City
FROM vw_Country
) sourcetable
PIVOT
(
MIN(City)
FOR Country IN ([Australia], [India], [United States])
) AS pvt
ORDER BY pvt.Rownum
Create another column by using rank then use pivot.
Select [Australia], [India], [United States]
from
(
select Country, City,
Rank() over (partition by country order by city) as rnk
from yourTable
) src
pivot
(
Max(City)
for Country in ([Australia], [India], [United States])
) piv;