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 > Using the Count expression

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-04, 11:53
iangilsenan iangilsenan is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Using the Count expression

How can I use the Count expression to achieve the following:

availability table
room_id property_id date_id available
1 8 1 Yes
1 8 2 Yes
1 8 3 Yes
2 8 1 Yes
2 8 2 No
2 8 3 Yes

Select the room ids that are available for all three date ids without using repetitive AND statements and instead using the following SQL with a count = 3 expression.

SELECT room_id
FROM availability
WHERE availability.property_id=8
AND availability.available=Yes
AND date_id BETWEEN 1 AND 3

This returns:
room_id
1
1
1
2
2

but I only want to return the room_ids that are available for all 3 date_ids i.e. room_id 1.

so I could use the DISTINCT key word but this would return:
room_id
1
2

so I need to also say in an expression "where count each room_id = 3".
Reply With Quote
  #2 (permalink)  
Old 02-15-04, 16:32
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Query like this one does what you wanted, but I'm sure there must be a better way to do it ...
PHP Code:
SELECT b.room_id
  FROM availability b
  WHERE b
.property_id 
    
AND b.date_id BETWEEN 1 AND 3
  GROUP BY b
.room_id
  HAVING 3 
= (SELECT COUNT(*) FROM availability b1
              WHERE b1
.available 'Yes'
             
AND b1.room_id b.room_id
             
);

   
ROOM_ID
----------
         
1
1 row selected 
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