Re: Simple(?) sql question...
- From: carl <cag_ng@xxxxxxxxxxxxx>
- Date: Sun, 16 Jul 2006 04:22:42 GMT
Jonathan Ball wrote:
cg_news wrote:
"cg_news" <cag_ng@xxxxxxxxxxxxxxx> a ?crit dans le message de
news:mL%Mc.2553$AY5.1525@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello,
I have what I hope is a simple sql (DB2/AS400) question.
Assume 2 files exist. One is a transaction file with two fields, the
transaction ID and the transaction name. File two is keyed to file 1
through the transaction id and contains a transaction date. Since
transactions can be processed more than once, the transaction id can
appear in the second file any number of times, but at least once.
Now for my question. I need to be able to select all records in file 1
while joining to file 2 and selecting the most recent date. Am i making
any sense?
In other words, I need to see the transaction id, name, and most recent
date from file 2, but a simple join will return multiple instances of
the transaction ID where I want to only see each ID once?
Any help appreciated
I found it, with a little help from comp.databases.ibm-db2
In case anyone is interested, the relevant sql:
select distinct a.ID, a.NAME, b.DATE from MASTER a
, DETAIL b where a.ID = b.ID and
b.DATE = (select max(DATE) from DETAIL c where c.ID = a.ID)
You don't need DISTINCT (for what you're trying to do),
and in fact it will make your query perform worse.
You did, however, hit on the basic correct approach:
you have to determine the maximum transaction date in
the detail file. Here are two ways you might have
written the query; the first is pretty close to what
you did come up with. I changed the name of the date
field to tran_date, as DATE is a SQL reserved word.
select a.id, a.name, b.tran_date
from master a join detail b
on a.id = b.id
where b.tran_date =
(select max(tran_date)
from detail
where id = a.id)
Alternatively (and functionally equivalent):
select a.id, a.name, b.tran_date
from master join
(select id,max(tran_date) tran_date
from detail
group by id) b
on a.id = b.id
The reason you don't need the DISTINCT keyword is that
the ID column already is unique in table MASTER, and by
selecting only the maximum value for TRAN_DATE (for any
given ID) from the DETAIL table, you will have a
distinct value of ID from table DETAIL as well.
However, the query analyzer won't "know" that, and it
will go through some extra - and relatively costly -
steps to try to make the rows distinct, when they
already are.
Very Nice.
Thanks for the detailed and helpful explanation.
.
- References:
- Re: Simple(?) sql question...
- From: Jonathan Ball
- Re: Simple(?) sql question...
- Prev by Date: Re: Simple(?) sql question...
- Next by Date: Re: i5
- Previous by thread: Re: Simple(?) sql question...
- Next by thread: Re: SQL performance - multiple rows for SELECT (SQL0811)
- Index(es):
Relevant Pages
|