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 > Retrieve first record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-18-03, 12:24
kengreer kengreer is offline
Registered User
 
Join Date: Dec 2003
Posts: 3
Retrieve first record

I have a table of payments with these columns:

PaymentDate,UID,Item,Amount,Status,TransactionID

I'd like to retrieve the first instance of each purchase by a user (UID) where Status is "Completed".

For example

1/1/2003,10,Socks,7,Pending,1001
1/1/2003,10,Socks,7,Completed,1002
1/2/2003,11,Shirt,20,Pending,1003
1/2/2003,11,Shirt,20,Completed,1004
1/3/2003,11,Pants,30,Completed,1005

should return:

1/1/2003,10,Socks,7,Completed,1002
1/2/2003,11,Shirt,20,Completed,1004
Reply With Quote
  #2 (permalink)  
Old 12-18-03, 12:48
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
select columns
from table t
where rowid = (select min(rowid) from table where t.key = key);
__________________
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-18-03, 13:03
kengreer kengreer is offline
Registered User
 
Join Date: Dec 2003
Posts: 3
Quote:
Originally posted by r123456
select columns
from table t
where rowid = (select min(rowid) from table where t.key = key);
Thanks for the reply but forgive me for being a newbie. I tried

SELECT ID,PaymentDate,UID,AID,ItemNumber,Username,Payment Status,TxnID FROM transactions where UID = (select min(UID) from table where transactions.UID = UID)

and I get

[Microsoft][ODBC Microsoft Access Driver] Syntax error. in query expression 'UID = (select min(UID) from table where transactions.UID = UID)'.
Reply With Quote
  #4 (permalink)  
Old 12-18-03, 13:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select PaymentDate
     , UID
     , Item
     , Amount
     , Status
     , TransactionID
  from transactions   FOO
 where Status  = 'Completed'
   and PaymentDate
     = ( select min(PaymentDate)
           from transactions
          where Status  = 'Completed'
            and UID = FOO.UID )
rudy
http://r937.com
Reply With Quote
  #5 (permalink)  
Old 12-18-03, 14:26
kengreer kengreer is offline
Registered User
 
Join Date: Dec 2003
Posts: 3
Quote:
Originally posted by r937
Code:
select PaymentDate
     , UID
     , Item
     , Amount
     , Status
     , TransactionID
  from transactions   FOO
 where Status  = 'Completed'
   and PaymentDate
     = ( select min(PaymentDate)
           from transactions
          where Status  = 'Completed'
            and UID = FOO.UID )
rudy
http://r937.com
It's a winner! Thank you!

I thought this could be done with some form of TOP and/or GROUP BY but I spent all morning trying different incarnations with no luck. Thanks again 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