Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL Query having where clause in count

Hi,

I am trying to get count on a column with a condition.Any suggestion on how to use count having a where clause
example;
 WHERE (C.RegistrationNumber is null or C.RegistrationNumber =  '') and count the return value

Cheers
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try...

select c.yourfield,
sum(case when C.RegistrationNumber is null or C.RegistrationNumber =  '' then 1 else 0 end) yourCnt
from yourTable C
Group by c.yourfield

Open in new window

Avatar of RIAS

ASKER

This query just returns 1,0 ,need to have sum of the returned records.

The query is
Return employees who have more than 1 car  
Include a column that gives the number of cars they have registered


Cheers
how's your data looks like and what's your expected output?
try:

select c.yourfield,
count(C.RegistrationNumber) yourCnt
from yourTable C
where C.RegistrationNumber is not null and C.RegistrationNumber <> ''
Group by c.yourfield

Open in new window

Avatar of RIAS

ASKER

Cheers mate wiil try and get back !
Avatar of RIAS

ASKER

This query is errored

select E.EmployeeID,E.Firstname
count(C.RegistrationNumber) Number_of_Cars_Registered FROM
B35_Employees E  INNER JOIN B35_Cars C
       ON E.EmployeeID = C.EmployeeID
WHERE COALESCE(C.RegistrationNumber, '') <> ''
Group by C.EmployeeID
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For example..

Select athlete, year, Sum(annual salary)
FROM league_stats
GROUP BY athlete, year
HAVING Sum(AnnualSalary) > 1000000   -- that made over a million bucks in their careers
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Cheers Experts!