473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Count(*)

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.

Thanks
Nov 12 '05 #1
2 6652
Ian
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! =-----
Nov 12 '05 #2
A UNION should work, although with no DDL or sample rows for
AIMD.AIMQUERY, I have had to invent some junk about the sample
database's EMPLOYEE table - no idea whether this logic is sound (it's
Friday afternoon after all):

SELECT 'UNFULFILLED REQUESTS:' as Status, COUNT(1) as Number
FROM employee
WHERE salary>500 OR Empno is not null OR workdept IS NOT NULL
union
SELECT 'FULFILLED REQUESTS:' as Status, COUNT(*) as Number
FROM employee
WHERE phoneno IS NULL OR hiredate IS NULL OR job IS NULL

STATUS NUMBER
--------------------- -----------
FULFILLED REQUESTS: 0
UNFULFILLED REQUESTS: 32

2 record(s) selected.

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.

Thanks


Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
6
by: Geetha | last post by:
I searched in the Oracle documents what count (1) meant and I could not find an answer. Can some one explain what Oracle does internally when use count (1) VS count (*). Thank you very much in...
1
by: JD | last post by:
Hi guys I'm trying to write a program that counts the occurrences of HTML tags in a text file. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h> ...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
23
by: Gary Wessle | last post by:
Hi I have a vector<charwhich looks like this (a d d d a d s g e d d d d d k) I need to get the biggest count of consecutive 'd'. 5 in this example I am toying with this method but not sure if...
1
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
7
by: Chris | last post by:
I am trying to increase/decrease the value of $_SESSION by 1 after clicking on a link e.g index.php?gotoWk=nxtWk and index.php? gotoWk=lstWk. I'm sure you will get the drift if you look at the code...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.