Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 12890

Convert columns into rows in a dataset

$
0
0

Hello,

I am working on a query whihc actually shows the count of countries for each continent. This is my query

Createtable continent   (ID INT, Name VARCHAR(30))INSERT continents VALUES(1,'Asia'),(2,'Africa'),(3,'North America'),(4,'South America')Createtable  country   (ID INT, Name VARCHAR(30), ContinentID INT, Population INT)INSERT country   VALUES(1,'China',1,2000),(2,'India',1,1500),(3,'Ethiopia',2,7500),(4,'United States',3,5000)
Select ct.Name,
  Sum(Case When cy.Population <= 2000 Then 1 Else 0 End) As LE2000Count,
  Sum(Case When cy.Population > 2000 Then 1 Else 0 End) As GT2000Count
From Continent ct
Left Join Country cy On ct.ID = cy.CountinentID;

when I execute the above query I am getting the result as

Name            LE2000Count               GT2000Count

Asia                2                                        0

Africa              0                                         1

North America  0                                      1

South America 0                                      0

How can show the counts in seperate rows instead of columns? Please help. 

Thank you.


Viewing all articles
Browse latest Browse all 12890

Latest Images

Trending Articles



Latest Images