Anthony Robinson wrote:
Is there any way to do multiple select count(*) statements in a SQL
query. I have a table that I want to get the counts of rows matching
certain criteria. For example:
SELECT COUNT(1) AS "UNFULFILLED REQUESTS" FROM AIMD.AIMQUERY WHERE
ERRORSEVERITY IS NOT NULL OR ERRORMESSAGE IS NOT NULL OR ERRORTYPE IS
NOT NULL AND (SELECT COUNT(*) AS "FULFILLED REQUESTS" FROM
AIMD.AIMQUERY WHERE ERRORSEVERITY IS NULL OR ERRORMESSAGE IS NULL OR
ERRORTYPE IS NULL)
I would like to do this in one statement if possible I figured I could
do it in a stored proc (create temp table and populate with the
counts).
Any insight would be appreciated.
Here's how you would do this, based on the logic you gave above:
select
sum(case when (ERRORSEVERITY IS NOT NULL OR
ERRORMESSAGE IS NOT NULL OR
ERRORTYPE IS NOT NULL) then 1 else 0 end) as unfulfilled,
sum(case when (ERRORSEVERITY IS NULL OR
ERRORMESSAGE IS NULL OR
ERRORTYPE IS NULL) then 1 else 0 end) as fulfilled
from
aimd.aimquery;
Now, keep in mind that your query as written has a logic error (take a
where errorseverity = 1, errormessage = 'Error' and errortype is null:
both columns match, and the record counts as both fulfilled and unfulfilled).
Fulfilled is presumably where
ERRORSEVERITY IS NULL AND
ERRORMESSAGE IS NULL AND
ERRORTYPE IS NULL
but you should check this to make sure.
Good luck,
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----