Combining Few Rows of data in One ROW using Sum Function and not doing this for all the row
Fiends Please help me with this ASAP. Really appreciate it thanks
I have really simple table. Which as three columns
Col A Col B Col C
(unique)nameA (UniqueID)1 (somenumber)10
(unique)nameB (UniqueID)2 (somenumber)20
(unique)nameC (UniqueID)3 (somenumber)30
(unique)nameD (UniqueID)4 (somenumber)10
(unique)nameE (UniqueID)5 (somenumber)50
(unique)nameF (UniqueID)6 (somenumber)35
(unique)nameG (UniqueID)7 (somenumber)50
(unique)nameH (UniqueID)8 (somenumber)10
(unique)nameI (UniqueID)9 开发者_开发问答 (somenumber)25
As per my report requirement i need to combine Unique ID (1,2,3) Give it a Unique name ALFA and Sum the Col C values AND combine (4,5,6) Give it a Unique name BETA and Sum the Col C values for them And keep other Unique ID, Unique Name and Their values in Col C as is Order by values in Col C Desc and display TOP 30 results.
So Final result should look like this
Col A Col B Col C
BETA (unique ID 4,5,6) 95 --(10+50+35 from col C
ALFA (unique ID 1,2,3) 60 --(10+20+30 from col C above)
above)
(unique)nameG (UniqueID)7 (somenumber)50
(unique)nameI (UniqueID)9 (somenumber)25
(unique)nameH (UniqueID)8 (somenumber)10
ould something like this work?
declare @simpletable table( name varchar(50), uniqueid int, somenumber int)
insert into @simpletable
select 'a', 1, 10
union all
select 'b', 2, 20
union all
select 'c', 3, 30
union all
select 'd', 4, 10
union all
select 'e', 5, 50
union all
select 'f', 6, 35
union all
select 'g', 7, 50
union all
select 'h', 8, 10
union all
select 'i', 9, 25
select top 30 name,uniqueid,somenumber
from
(
select 'ALFA' as name, '1,2,3' as uniqueid, sum(somenumber) as somenumber
from @simpletable
where uniqueid between 1 and 3
union all
select 'BETA' as name, '4,5,6' as uniqueid, sum(somenumber) as somenumber
from @simpletable
where uniqueid between 4 and 6
union all
select name as name, cast(uniqueid as varchar(50)) as uniqueid, somenumber as somenumber
from @simpletable
where uniqueid > 6
) as x
order by somenumber desc
精彩评论