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 |
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2010-05-05 : 03:20:00
|
| i have data like this...CREATE TABLE Try1(tblname varchar(10),Createdby varchar(10))insert into Try1select 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1','user1' UNION ALLselect 'Temp1',NULL UNION ALLselect 'Temp1',NULL I want results like thistblname cnt agncntTemp1 9 12is there any other way to achieve this...i used it like this..select tblname, SUM(case when createdby IS null then 1 else 0 end) as cnt, SUM(case when createdby is not null then 1 else 0 end) as agncnt from Try1 group by tblnameis there any simple way...iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2010-05-05 : 03:24:38
|
| select tblname,SUM(case when createdby IS null then 1 else 0 end) as cnt,count(createdby) as agncnt from Try1 group by tblnameKarthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-05 : 03:26:34
|
What is wrong with this solution?It looks simple enough. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2010-05-05 : 03:34:24
|
quote: Originally posted by webfred What is wrong with this solution?It looks simple enough. No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks fred for your reply...Am just hunting for another way, i want to remove that case clause from this query and this is just for learning purpose, trying to thinking out of box.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2010-05-05 : 03:39:20
|
quote: Originally posted by ashishashish
quote: Originally posted by webfred What is wrong with this solution?It looks simple enough. No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks fred for your reply...Am just hunting for another way, i want to remove that case clause from this query and this is just for learning purpose, trying to thinking out of box.iF theRe iS a wAy iN tHen theRe iS a wAy oUt..
U tried this one..select tblname,SUM(case when createdby IS null then 1 else 0 end) as cnt,count(createdby) as agncnt from Try1 group by tblnameKarthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-07 : 09:48:49
|
| If you want to completely avoid CASE expressionselect tblname,count(*)-count(Createdby) as cnt ,count(Createdby) as agncntfrom Try1group by tblnameMadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-07 : 09:54:33
|
quote: Originally posted by madhivanan If you want to completely avoid CASE expressionselect tblname,count(*)-count(Createdby) as cnt ,count(Createdby) as agncntfrom Try1group by tblnameMadhivananFailing to plan is Planning to fail
I always enjoy to read your easy solutions which I have not seen because I am thinking too complicated.madhi, you're cool  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-07 : 10:41:35
|
quote: Originally posted by webfred
quote: Originally posted by madhivanan If you want to completely avoid CASE expressionselect tblname,count(*)-count(Createdby) as cnt ,count(Createdby) as agncntfrom Try1group by tblnameMadhivananFailing to plan is Planning to fail
I always enjoy to read your easy solutions which I have not seen because I am thinking too complicated.madhi, you're cool  No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|