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 > Database Server Software > DB2 > Query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-04, 13:19
mike_moran mike_moran is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Query help

I have two tables (queue1 & queue2). They both have
the same columns (acct, date & amt). There are many
records for the same accounts in both tables.

I need to select a list of rows using a like operator
returning only the newest record for each acct.

Queue1
Acct Date Amt
12345 1/1/2004 100.00
1234 1/1/2004 200.00
123 7/21/2004 250.00
123456 7/21/2004 150.00

Queue2
Acct Date Amt
1234 1/1/2004 100.00
12345 4/1/2004 200.00
123456 7/20/2004 300.00
123456 7/10/2004 50.00


Acct like ‘12345%’ should return
Acct Date Amt
12345 4/1/2004 200.00
123456 7/21/2004 150.00

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 07-21-04, 13:42
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
You can use the NOT EXISTS of the WHERE clause to create a subquery. Check the SQL Reference for details.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 07-21-04, 17:38
mike_moran mike_moran is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
This works. Any one know of a more elegent way?

SELECT
q.acct,
q.trans_date,
q.amt
FROM
(SELECT acct, trans_date, amt FROM QUEUE1 q1
WHERE
q1.acct LIKE '12345%'
UNION ALL
SELECT acct, trans_date, amt FROM QUEUE2 q2
WHERE q2.ACCT LIKE '12345%') q
WHERE
q.TRANS_DATE = (SELECT MAX(q4.trans_date)
FROM
(SELECT acct,trans_date FROM QUEUE1 q1
WHERE
q1.acct LIKE '12345%'
UNION ALL
SELECT acct,trans_date FROM QUEUE2 q2
WHERE q2.ACCT LIKE '12345%') q4
WHERE q.acct = q4.acct)
Reply With Quote
  #4 (permalink)  
Old 07-22-04, 10:42
saltbits saltbits is offline
Registered User
 
Join Date: Jun 2004
Posts: 28
how about:

with tmp as (select * from queue1 where acct like '12345%' union all select * from queue2 where acct like '12345%') select * from tmp
where dat in (select max(dat) from tmp group by acct)
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