Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello, I have table like this


country | State | population(in Millions)
India | Ap | 5
India | Mp | 6
Us | mUscha| 10
US | NewYoek| 6
Us | mUscha| 10

i want out put lile

country | State | population(in Millions)
India | Ap | 5
| Mp | 6

11
Us | mUscha| 10
| NewYoek| 6
| mUscha| 10

26

country display only in first row and total of population display particlular country end group next row

can u guide or send any snippets
Posted
Updated 4-Dec-12 1:08am
v3
Comments
yaser shadmehr 4-Dec-12 7:09am    
what is your DB? (SQL language )? PLSQL (oracle) or SQL Server?
kingsa 6-Dec-12 0:33am    
sqlserver

1 solution

Hi Suhelsa,
Nice Question

I have done this in 3 Steps

1) Extract Data into Temporary table #temp

/* 
assuming Your Table Name as - TblCountry
*/

Select * into #temp from TblCountry


2) Insert Total Population Rows in Temporary Table

SQL
insert into #temp
select country,''state, SUM(Population)Population 
from #temp
group by country



3) Set Rank Number on the basis of contry and Order by State

SQL
select Rank() over (Partition By #temp.Country order by #temp.Country , state desc) row , * 
into #tempOutput 
from #temp


4) Update #tempOutput set Country Name ='' where Row > 1
SQL
update #tempOutput set Country ='' where ROW > 1


5) Select from Output Table and drop Temporary table Created
SQL
Select * from #tempOutput 
drop #tempOutput 
drop #temp


Please vote for My Solution
Thanks !
Yogendra Dubey
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900