| Author |
Topic |
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2009-11-03 : 07:02:57
|
| Hi all,NameabcbcdabcabcbcdIn above table abc has repeated 3 times and bcd as 2 so on.output expected asName Countabc 3bcd 2Total 5I have tried row_number() fn and all that i could not count these repeated counts.at last Total field to be added and corresponding count be added like i shown in the o/p sec..pl help me to sort out this. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-03 : 07:11:23
|
| select name,count(*) from your_tablegroup by nameMadhivananFailing to plan is Planning to fail |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2009-11-03 : 07:16:32
|
| Thanks madivanan,i was confused.i need to add Total and count at the last..pl see my post once i again pl. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-03 : 07:18:15
|
quote: Originally posted by haroon2k9 Thanks madivanan,i was confused.i need to add Total and count at the last..pl see my post once i again pl.
Where do you want to show the result?MadhivananFailing to plan is Planning to fail |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2009-11-03 : 07:19:43
|
| At the end of the Result like belowName Countabc 3bcd 2Total 5 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-03 : 07:31:07
|
quote: Originally posted by haroon2k9 At the end of the Result like belowName Countabc 3bcd 2Total 5
I meant if you wanted to show the result in any front end applicationMadhivananFailing to plan is Planning to fail |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2009-11-03 : 07:37:20
|
| Not like that, select name,count(*) from your_tablegroup by nameIf i execute it,its returns the resultset.now i need to show the Text like Total at the end of name Field and total count as to show at the end of the count field as like this.Name Countabc 3bcd 2Total 5 |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-11-03 : 07:53:24
|
| Try Like this,select val,count(val) as sum_val from #temp group by val unionselect 'total',sum(sum_val) from (select val,count(val) as sum_val from #temp group by val) tBut Front end does it better!Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-03 : 07:59:03
|
selectcase when (GROUPING([NAME])=1) then 'Total' else [Name]end as [Name],count(*) as [Count]from your_tablegroup by [Name]with cube No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-03 : 08:29:22
|
quote: Originally posted by senthil_nagore Try Like this,select val,count(val) as sum_val from #temp group by val unionselect 'total',sum(sum_val) from (select val,count(val) as sum_val from #temp group by val) tBut Front end does it better!Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
Val should be converted to VARCHAR to fit with 'total'MadhivananFailing to plan is Planning to fail |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-11-03 : 08:36:00
|
quote: Originally posted by madhivanan
quote: Originally posted by senthil_nagore Try Like this,select val,count(val) as sum_val from #temp group by val unionselect 'total',sum(sum_val) from (select val,count(val) as sum_val from #temp group by val) tBut Front end does it better!Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
Val should be converted to VARCHAR to fit with 'total'MadhivananFailing to plan is Planning to fail
Ya ofcourse here val is a varchar files which hold 'abc','bcd' ect.Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
dhextercorpuz
Starting Member
1 Post |
Posted - 2009-12-08 : 15:47:06
|
| SELECT NAME, COUNT(*) AS COUNT FROM YOURTABLEGROUP BY NAMEUNIONSELECT 'TOTAL' AS NAME, COUNT(*) AS COUNT FROM YOURTABLESharing of knowledge is the most noble things to do... |
 |
|
|
vikky
Yak Posting Veteran
54 Posts |
Posted - 2009-12-09 : 04:16:34
|
| hi,declare @tab table (name varchar(12))insert into @tabselect 'abc' union all select 'bcd' union all select 'abc' union all select 'abc' union all select 'bcd'select count(name),namefrom @tabgroup by namecompute sum(count(name) )Thanks,vikky. |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-12-09 : 06:26:35
|
| Compute will be depricated with the next version of SQL SERVER. It is recommended that you do not use it.Fred gave an answer a MONTH ago. Any particular reason to dig this up?http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|