Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
anabelle
Starting Member
19 Posts |
Posted - 2009-01-30 : 01:28:59
|
Need Help, how wud i code to display this sample table, tablename:fruitsG1 G2 CountofApplesApple A 1Apple B 2Apple C 4Total 7Orange A 10Orange B 10Orange C 10Total 30Let me know how wud i do it?i really need a little help...if someone does can help me u can write it or reply me on my email...thank u.. |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-30 : 01:33:54
|
| try like thisselect g1,g2,sum(countofapples)over (partition by g1 order by g1) from fruits |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-01-30 : 02:17:20
|
quote: Originally posted by bklr try like thisselect g1,g2,sum(countofapples)over (partition by g1 order by g1) from fruits
How do you think that your query is going to give the desired output?Anabelle try thisdeclare @table as table(g1 varchar(10),g2 varchar(5),cnt int)insert into @tableselect 'Ap','A',1 union allselect 'Ap','B',2 union allselect 'Ap','C',4 union allselect 'Or','A',10 union allselect 'Or','B',10 union allselect 'Or','C',10;with cte(g1,g2,cnt,row)as(select g1,g2,cnt,dense_rank()over(order by g1) from @table union all select cast(null as varchar(10)),cast('Total' as varchar(10)),sum(cnt),dense_rank()over(order by g1) from @tablegroup by g1)select g1,g2,cnt from cte order by row,g1 desc |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-30 : 02:25:56
|
| [code]SELECT G1,G2,COUNT(*) AS CntFROM TableGROUP BY G1,G2WITH ROLLUP[/code] |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-01-30 : 02:40:37
|
[code]declare @table as table(g1 varchar(10),g2 varchar(5),cnt int)insert into @tableselect 'Ap','A',1 union allselect 'Ap','B',2 union allselect 'Ap','C',4 union allselect 'Or','A',10 union allselect 'Or','B',10 union allselect 'Or','C',10SELECT case when grouping(G1)=0 then G1 else 'total' end as G1,case when grouping(G2)=0 then G2 else 'total' end as G2,sum(cnt) AS CntFROM @tableGROUP BY G1,G2WITH ROLLUP[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
anabelle
Starting Member
19 Posts |
Posted - 2009-01-30 : 03:33:31
|
| hello everyone i am so happy with all ur answers, this is my first time to joine the forums and this is great! i wud surely learn a lot from here,i will try those codes later...thanks a lot for a great help it gives me an idea, i am a bit beginner on on my job so i am trying still to cope up and understand everyhting... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-30 : 03:38:36
|
| welcome |
 |
|
|
|
|
|
|
|