If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > very stuck on my newbie query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-03, 10:44
simple_simon simple_simon is offline
Registered User
 
Join Date: Dec 2003
Posts: 16
Red face very stuck on my newbie query

Hi

I'm very stuck with this query, any help would be much appreciated i'm very new to SQL. Basically I need the query to give me a list of hospitals that have less than a given number of patients (for example less than 2) within a certain state (for example 001).

My current attempt is:

select count(hospital_no)quantity,hospital_no
from Records_table
where state_no = '001'
and where quantity < '2'
group by species_no

but I cant see where I am going wrong, I think i may need to add a subquery somehow? In the above code, 'quantity' is a name given to the result of the count as shown. I have tried to reference it later to say give me all records with less than 2 for quantity but it doesnt take it.

I am using SQL Query Analyzer for this project.
Reply With Quote
  #2 (permalink)  
Old 12-16-03, 11:01
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Try this,

select species_no, count(*)
from Records_table
where state_no = '001'
group by species_no
having count(*) < 2
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 12-16-03, 11:08
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
You were close. Try this:

Code:
SELECT Count(hospital_no) Quantity, Hospital_no
FROM records_table
WHERE state_no = '001'
GROUP BY hospital_no
HAVING Count(hospital_no) > 2;
JoeB
Reply With Quote
  #4 (permalink)  
Old 12-16-03, 11:12
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
"> 2" ?

Would it not be <= 2?
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #5 (permalink)  
Old 12-16-03, 19:31
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
Yes, kinda fat-fingered that one. Good catch.
Reply With Quote
  #6 (permalink)  
Old 12-17-03, 05:24
simple_simon simple_simon is offline
Registered User
 
Join Date: Dec 2003
Posts: 16
that is excellent, thanks so much for your help!
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

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