Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > count query problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-11-04, 17:10
bmeansdfw bmeansdfw is offline
Registered User
 
Join Date: Jun 2004
Posts: 4
count query problem

I have a table with a list of clients, some tasks associated with each and the status of each task (open, closed).

I want to return a SINGLE row for each client, along with a COUNT of the number of "open" and "closed" tasks.

The typical count statement I am using is this:

select SiteName, Count(IssueStatus) as cnt, IssueStatus,
from IssueMaster
group by SiteName, IssueStatus

It works fine, however, it returns a row for each combination of client and status. I would like it to return "Client, Count of Open, Count of Closed".

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 06-11-04, 17:46
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,573
It depends on which SQL dialect you are using, but I'd use Sum() combined with CASE instead of Count().

-PatP
Reply With Quote
  #3 (permalink)  
Old 06-14-04, 10:22
bmeansdfw bmeansdfw is offline
Registered User
 
Join Date: Jun 2004
Posts: 4
I'm using MSSQL, but can I SUM a non-numeric column? And do you have an example I could use as a model?

Thanks!
Reply With Quote
  #4 (permalink)  
Old 06-14-04, 10:50
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Like this:

SUM (CASE WHEN status='OPEN' THEN 1 ELSE 0 END) AS open_count
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 06-14-04, 10:52
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Or still using COUNT:

COUNT (CASE WHEN status='OPEN' THEN 1 END) AS open_count
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On