Multiple count results in Query with different WHERE conditions.

Posted on 2007-08-09
Medium Priority
Last Modified: 2013-11-05
Hi there..

The language is c# but this question is query specific though..

Ok.. i'm creating a query which will return some statistics, and what's special in this case.. is that one count must include all the records, while another one.. will only count for a specific set of records.. i'll describe with kvasi-sql here :

PS ! the --> (where xxxxx = "xxx") <-- is explaining the where situation i need pr count.

select count(numberofcontracts) AS amountCon,
count(status) AS acceptedCon--> (where status = "accepted") <--,
count(status) AS notAccepted --> (where status = "notaccepted") <--,
count (progress) --> (where progress = "development") <--
from table where costumernumber = 'something'.

Hope i explained me well enough :)

Best regards
Question by:Kripos56
LVL 53

Expert Comment

ID: 19661784
Do you need to return the values all at once ?
You can use a union, something like this:
select 'All', count(numberofcontracts) AS amountCon
select 'Accepted',  count(status) AS amountCon where status = "accepted"
LVL 11

Accepted Solution

Sven earned 1000 total points
ID: 19661801
(select count(numberofcontracts) from table where costumernumber = 'something') AS amountCon,
(select count(status) from table where costumernumber = 'something' and status = "accepted") AS acceptedCon,
(select count(status) from table where costumernumber = 'something' and status = "notaccepted") AS notAcceptedCon,
(select count(progress) from table where costumernumber = 'something' and progress = "development") AS  DevelopmentCon
LVL 58

Expert Comment

ID: 19663176
You can also sum boolean values.

select count(numberofcontracts) AS amountCon,
sum(status = 'accepted') AS acceptedCon,
sum(status = 'notaccepted') AS notAccepted,
sum(progress = 'development')
from table where costumernumber = 'something'

Depending on the coding for True (it's -1 in JetSQL, for example), you'll need to add a minus sign or use abs():

-sum(status = 'accepted') AS acceptedCon,
abs(sum(status = 'accepted')) AS acceptedCon,


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question