MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 4th, 2004, 05:38 AM
gobears03 gobears03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 gobears03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question about HAVING clause

Hello,

I'm working on this query that's supposed to give me a list of registered user's last_name, first_name, id and also the count on the number of visits during a time period:

Quote:
SELECT DISTINCT person.last_name AS l_name, person.first_name
AS f_name, person.id AS idnum, count (*) AS num_visits
FROM person, trail
WHERE person.last_name IS NOT NULL AND
person.id = trail.person_id AND
EXISTS
(select person.id, count (*) from person, trail group by
person.id HAVING count(*) >= 2 )
AND (trail.creation_date BETWEEN 'Jan 1 1997 00:00AM'
AND 'Jan 1 2000 11:59PM')
GROUP BY person.id, person.last_name, person.first_name
ORDER BY person.last_name, person.first_name ASC ;



However, if I change the count(*) >= 2 to be count(*) > 20 for example, it gives me the same rows as would >=2.

I would appreciate any insight. Thanks very much!

Reply With Quote
  #2  
Old August 4th, 2004, 06:09 AM
pickledshark pickledshark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Blackpool
Posts: 22 pickledshark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 32 sec
Reputation Power: 0
Send a message via ICQ to pickledshark Send a message via MSN to pickledshark
Quote:
AND EXISTS (select person.id, count (*) from person, trail group by
person.id HAVING count(*) >= 2 )


tie the two tables together in this subquery:

Quote:

AND EXISTS (select person.id, count (*) from person, trail
WHERE person.id = trail.person_id
group by person.id
HAVING count(*) >= 2 )

Reply With Quote
  #3  
Old August 4th, 2004, 06:20 AM
gobears03 gobears03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 gobears03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by pickledshark
tie the two tables together in this subquery:


Hm.. after testing your proposed solution, it doesn't seem to change the results. ie. >=2 still gives results that have a frequency = 1....

But thanks a lot for your response, pickledshark

Reply With Quote
  #4  
Old August 4th, 2004, 07:31 AM
pickledshark pickledshark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Blackpool
Posts: 22 pickledshark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 32 sec
Reputation Power: 0
Send a message via ICQ to pickledshark Send a message via MSN to pickledshark
When you say frequency, is this a column? or is this the number of records in the table 'trail' for a given person?

Reply With Quote
  #5  
Old August 4th, 2004, 04:17 PM
gobears03 gobears03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 gobears03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It is a column in the results table, coming from the count(*) query. But in other words, yes: the number of records in the table 'trail' for a given person record in the person table.


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Question about HAVING clause


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT