| Author |
Topic |
|
jbezanson
Starting Member
35 Posts |
Posted - 2006-06-26 : 10:13:00
|
I have the following query that gives me part of what I want but fully. I am using the Northwind database in SQL Server 2000SELECT Country, COUNT(Country) AS '# of Reps', ( SELECT COUNT(*) FROM Customers WHERE ContactTitle='Owner' ) AS 'Owners'FROM CustomersGROUP BY Country It selects the country and # of Reps correctly but the 'Owners' column always says 17 (it is counting every record not just for each country). I am not sure how to do this.Thanks for your helpJustin Bezansonjbezanson@spatterdesign.cawww.aspnetguy.com |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-26 : 10:16:30
|
| [code]SELECT Country, COUNT(*) [# of Reps], SUM(CASE WHEN ContactTitle = 'Owner' THEN 1 ELSE 0 END) OwnersFROM CustomersGROUP BY CountryORDER BY Country[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
jbezanson
Starting Member
35 Posts |
Posted - 2006-06-26 : 10:18:48
|
| Thanks so much PesoJustin Bezansonjbezanson@spatterdesign.cawww.aspnetguy.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-26 : 10:23:32
|
orSELECT Country, COUNT(Country) '# of Reps', COUNT(CASE WHEN ContactTitle = 'Owner' THEN 1 END) 'Owners'FROM CustomersGROUP BY Country MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-26 : 10:29:46
|
quote: Originally posted by madhivanan
SELECT Country, COUNT(Country) '# of Reps', COUNT(CASE WHEN ContactTitle = 'Owner' THEN 1 END) 'Owners'FROM CustomersGROUP BY Country
I wouldn't since the message Warning: Null value is eliminated by an aggregate or other SET operation. is sent back to front-end application and could cause unpredicted results, depending on error handling.Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-26 : 10:32:52
|
| Though you use SUM(CASE...), you get same warning. Right?MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-26 : 10:35:45
|
quote: Originally posted by madhivanan Though you use SUM(CASE...), you get same warning. Right?
No. Only (21 row(s) affected) is returned but this can be avoided and should be, with SET NOCOUNT ON.It's a good practice from ASP and Visual Basic to always use SET NOCOUNT ON, because in certain circumstances the message (21 row(s) affected) is sent to the client.Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-26 : 10:45:44
|
Well. If Country is not null, you wont get the warning otherwise you willDeclare @Customers table(country varchar(100), ContactTitle varchar(100))Insert into @Customers Select 'India', 'Manager' union all Select 'India', 'Owner' union all Select 'England', 'Manager' union all Select 'India', NULL union all Select NULL, 'Owner' SELECT Country, COUNT(Country) '# of Reps', SUM(CASE WHEN ContactTitle = 'Owner' THEN 1 ELSE 0 END) 'Owners'FROM @CustomersGROUP BY CountrySELECT Country, COUNT(Country) '# of Reps', COUNT(CASE WHEN ContactTitle = 'Owner' THEN 1 END) 'Owners'FROM @CustomersGROUP BY Country MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-26 : 11:46:51
|
quote: Originally posted by madhivanan Well. If Country is not null, you wont get the warning otherwise you will
True. But changingCOUNT(Country) '# of Reps' toCOUNT(*) '# of Reps' will take care of that. See the different outputs too!Using COUNT(*) displaysCountry # of Reps Owners------- --------- ------NULL 1 1England 1 0India 3 1 which is correct, and using COUNT(Country) showsCountry # of Reps Owners------- --------- ------NULL 0 1England 1 0India 3 1 which doesn't count # of Reps well and produces the warning as well.Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-27 : 01:49:39
|
Yes. What I wanted to tell you is the warning may come from various methods and not just by using COUNT(CASE...) MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 02:21:25
|
| Of course. That's why the discussions here are meaningful and learning.COUNT(NULL) will always produce an error since NULL is a not known value. But SUM(1) or SUM(0) will never.Peter LarssonHelsingborg, Sweden |
 |
|
|
|