Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
How to count entries meeting a criteria?
Old 12-17-2008, 10:29 AM How to count entries meeting a criteria?
Junior Talker

Posts: 4
Trades: 0
Please be gentle! - this is my first post and I'm a total SQL novice thrown in at the deep end at work.

I need to output something like:

Name1 count#1 count#2 count#3 count#4
Name2 count#1 count#2 count#3 count#4
etc...

count#1 needs to be all the entries with a given "Name"
count#2 needs to be the subset of those that also has field#2 = value#1
count#3 needs to be the subset of those that also has field#2 = value#2
etc...

I can get something like:

SELECT
Name,
otherData
FROM
DBName
WHERE
field#3= value AND field#4 NOT IN ('valueA', 'valueB', 'valueC')
ORDER BY
Name

And that returns the subset I want to count, but as soon as I try replacing the "otherData" in the SELECT with:

count (when field#2=value#2 then 1 else 0 end) as count#1,
count (when field#2=value#3 then 1 else 0 end) as count#2,
count (when field#2=value#4 then 1 else 0 end) as count#3,

and adding this at the end:

GROUP BY
Name

Then I get totally silly counts.

I'm going round in circles trying to fix this, and no amount of web surfing is giving me any real clues. HELP!!!
petroldave is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-17-2008, 10:41 AM Re: How to count entries meeting a criteria?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I would suggest that a UNION would work
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-17-2008, 11:08 AM Re: How to count entries meeting a criteria?
Junior Talker

Posts: 4
Trades: 0
Quote:
Originally Posted by chrishirst View Post
I would suggest that a UNION would work
Would that solve the silly counts I'm getting?

e.g. when I run a simpe query I see that 3 entries match the Name1 count#1 criteria, but what I get when I include the count (when...) is 41 and not 3.
petroldave is offline
Reply With Quote
View Public Profile
 
Old 12-17-2008, 11:15 AM Re: How to count entries meeting a criteria?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
in theory it should, because all you need to do would be to replicate your first working query in each union with different criteria.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-17-2008, 11:21 AM Re: How to count entries meeting a criteria?
Junior Talker

Posts: 4
Trades: 0
Quote:
Originally Posted by chrishirst View Post
all you need to do would be to replicate your first working query
but I haven't got even one of the count() returning a correct value...
petroldave is offline
Reply With Quote
View Public Profile
 
Old 12-17-2008, 11:35 AM Re: How to count entries meeting a criteria?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
add;

SELECT COUNT(column) AS rows, rest_of_fields

to the query that returns the correct subset
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-17-2008, 11:55 AM Re: How to count entries meeting a criteria?
Junior Talker

Posts: 4
Trades: 0
The count needs to be conditional - as per my first post...
petroldave is offline
Reply With Quote
View Public Profile
 
Old 12-17-2008, 02:37 PM Re: How to count entries meeting a criteria?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Chris gave you a working answer, and a link to documentation that will fill in all the blanks. Put your criteria in a where clause in the query Chris spelled out for you to make it conditional, and then use the UNION operator to bring in your other counts.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to count entries meeting a criteria?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.30578 seconds with 12 queries