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 > Pulling data across multiple tables.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-28-04, 04:53
escape escape is offline
Registered User
 
Join Date: Jan 2004
Location: USA
Posts: 3
Red face Pulling data across multiple tables.

I am attempting to pull all users over the age of 30 from a table. I want the results to be returned off of their membership status and recent activity. Their membership status is stored in a seperate table (Payment). Some users have more than one membership status record, while others might not have any at all.

LEFT JOIN seems like a possible solution, however, if a user has more than one membership status record, it is returning multiple results for that user (when I only want 1 result for that user).

Here is the code that I have so far.

SELECT Users.UserID FROM Users LEFT JOIN Payment ON Users.UserID = Payment.UserID AND Users.Age > 30 AND Payment.PaymentExpirationDate > '3/28/2004' ORDER BY Payment.MembershipNumber DESC, Users.LastActive DESC

Payment.MembershipNumber - INT Field. This is the membership status for the user, if the user has any membership records.
Users.LastActive - Date/Time Field. This is the recent active for the user.

The data is being returned right now is:
UserID
=====
3
8
12
12
12
9
9
1
... and so on, when it should look like ...
UserID
=====
3
8
12
9
1

I hope this makes sense. Thanks for your time.

James
Reply With Quote
  #2 (permalink)  
Old 03-28-04, 08:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if a user has more than one membership status record, which one do you want?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-28-04, 14:13
escape escape is offline
Registered User
 
Join Date: Jan 2004
Location: USA
Posts: 3
I need the first membership status record that has a PaymentExpirationDate closest to '3/28/2004'.
Reply With Quote
  #4 (permalink)  
Old 03-29-04, 18:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
PHP Code:
select U.UserID 
     
U.lastActive
     
P.PaymentExpirationDate
  from Users U
left 
  join Payment P
    on U
.UserID 
     
P.UserID 
   
and P.PaymentExpirationDate 
     
'3/28/2004'
 
where U.Age 30 
   
and P.PaymentExpirationDate 
     
= ( select min(PaymentExpirationDate)
           
from Payment
          where UserID 
P.UserID
            
and PaymentExpirationDate 
              
'3/28/2004' )
order
    by U
.LastActive desc 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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