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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-09-23 : 08:03:53
|
| Emmanuel writes "Hi! I have a Customers table with these fields: CustomerID CustomerName I would like to know with a single query how many of my Customers are named John and how many are not. It seems a very basic query to me but I can't figure it out without using temporary tables or stored procedures.Regards, Emmanuel" |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-09-23 : 08:24:19
|
| SELECT COUNT(*) as CountofJohns FROM MyTable WHERE CustomerName = 'John' |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-09-23 : 08:27:24
|
| selectsum(case CustomerName when 'John' then 1 else 0 end),count(*) - sum(case CustomerName when 'John' then 1 else 0 end)from t |
 |
|
|
dsdeming
479 Posts |
Posted - 2003-09-23 : 08:32:05
|
| Yet another way.SELECT COUNT( s.CustomerName ), COUNT( s2.CustomerName ) AS John FROM TableName s LEFT OUTER JOIN ( SELECT * FROM TableName WHERE CustomerName = 'John' ) s2 ON s.PK = s2.PKDennis |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2003-09-23 : 09:09:32
|
quote: Originally posted by Stoad selectcount(*) - sum(case CustomerName when 'John' then 1 else 0 end)
Staod!  selectsum(case when CustomerName ='John' then 1 else 0 end),sum(case when CustomerName <> 'John' then 1 else 0 end),from t Vit vit vit! __________________Make love not war! |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-09-23 : 09:18:14
|
| select 'John', count(*) from t where CustomerName='John'union allselect 'Not John', count(*) from t where CustomerName<>'John' |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-09-23 : 09:23:22
|
| >Staod!LOL :) What is that staod?? |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2003-09-23 : 09:34:18
|
pfff..... Stoad! __________________Make love not war! |
 |
|
|
|
|
|