SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL 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 ASP Free Forums Sponsor:
  #1  
Old February 23rd, 2004, 02:20 PM
mark.zurada mark.zurada is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 mark.zurada User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to mark.zurada
subquery needed??

TABLE NAME: patient
ssn | Name | Date_Admitted
421-00-1111 | Mark | 2004-01-02
555-11-7777 | Toby | 2004-03-04


TABLE NAME: events
ssn | Event_type | Date_Visit
421-00-1111 | Monthly | 2003-10-10
421-00-1111 | Monthly | 2003-11-11
421-00-1111 | Monthly | 2003-12-12
421-00-1111 | Regular | 2004-01-01
421-00-1111 | Regular | 2004-01-10
555-11-7777 | Regular | 2004-01-10


I'm interested in getting this result (similar to patients table except including two count columns):
SSN | Name | Date_Admitted | Count_Regular | Count_Monthly
421-00-1111 | Mark | 2004-01-02 | 2 | 3
555-11-7777 | Toby | 2004-03-04 | 1 | 0

I have the information above attached to this message in html. It may be easier to view.

The result here is essentially the patients table with two extra columns. These extra columns have the results of a COUNT from the events table. One column is a count of the "monthly" visits of the corresponding ssn value and the other column is a count of the "regular" visits of the corresponding ssn value. The dates are relatively unimportant but if you want to help above and beyond the call of duty, show me how to limit the COUNT values by date (count only those events after a certain date).

This has been boggling me for quite a while now. Please help me learn - my email is here. Thanks!!

Reply With Quote
  #2  
Old February 23rd, 2004, 02:32 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
What type of DB are you running this against?

S_

Reply With Quote
  #3  
Old February 23rd, 2004, 02:41 PM
mark.zurada mark.zurada is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 mark.zurada User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to mark.zurada
mysql

mysql 4.0

Reply With Quote
  #4  
Old February 23rd, 2004, 02:52 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
I don't know MySQL, but here it is in SQL of MS Access. It should be similar, with possible a few syntax changes


SELECT Patient.SSN, Patient.Name, First(Patient.Data_Admitted) AS Data_Admitted, Sum(IIf([Events]![Event_Type]="Monthly",1,0)) AS Monthly, Sum(IIf([Events]![Event_Type]="Regular",1,0)) AS Regular
FROM Events INNER JOIN Patient ON Events.ssn = Patient.SSN
GROUP BY Patient.SSN, Patient.Name;


Results
SSN | Name | Data_Admitted | Monthly | Regular
421-00-1111 | Mark | 1/2/2004 | 3 | 2
555-11-7777 | Toby | 3/4/2004 | 0 | 1

S-

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > subquery needed??


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 5 hosted by Hostway
Stay green...Green IT