Need help joining tables and getting max value [message #188995] |
Tue, 22 August 2006 12:54 |
Rustican
Messages: 51 Registered: July 2006
|
Member |
|
|
Need help trying to join two tables together. I want to join table 1 with table 2 but only where Table2.date is greater than Table1.date.
If i do
select * from table1, table2
where T1.myID = T2.myId and T2.date > T1.date;
I'll end up with extra rows. How can i get rid of the extra rows so that i'll only get the maxium dates from table 2?
Tried using max(T2.date) > T1.date but it won't work.
Table1
myID Date
1 Jan 2002
2 Jan 2003
3 Jan 2001
Table2
myID Date
1 Jan 2002
1 Jan 2003
1 Jan 2004
2 Jan 2004
2 Jan 2005
3 Jan 2004
Wanted result
myID TBL1.Date TBL2.Date
1 Jan 2002 Jan 2004
2 Jan 2003 Jan 2005
3 Jan 2001 Jan 2004
|
|
|
|
Re: Need help joining tables and getting max value [message #189064 is a reply to message #188995] |
Wed, 23 August 2006 01:50 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Don't use restricted words (such as DATE, NUMBER etc) as column names - it causes no end of problems.
An analytic solution would work well here.
SELECT distinct t1.myid
,t1.date_col
,max(t2.date_col) over (partition by t1.myid)
FROM table_1 t1
,table_2 t2
WHERE t1.myid = t2.myid;
|
|
|
|
|
Re: Need help joining tables and getting max value [message #189187 is a reply to message #188995] |
Wed, 23 August 2006 09:49 |
Rustican
Messages: 51 Registered: July 2006
|
Member |
|
|
Thanks for all the help. The queries from JRowbottom work fine but i made a mistake in what my results needed to be.
I realized that what i wanted was to list out the dates from table1 and the dates from table2 where the table2.date are greater than table1.date but are the earliest avaliable value in table2.dates
revised wanted results
myID TBL1.Date TBL2.Date
1 Jan 2002 Jan 2003
2 Jan 2003 Jan 2004
3 Jan 2001 Jan 2004
Sorry for the confusion and thank you all again very much for all the help.
Rustican
Rustican wrote on Tue, 22 August 2006 12:54 | Need help trying to join two tables together. I want to join table 1 with table 2 but only where Table2.date is greater than Table1.date.
If i do
select * from table1, table2
where T1.myID = T2.myId and T2.date > T1.date;
I'll end up with extra rows. How can i get rid of the extra rows so that i'll only get the maxium dates from table 2?
Tried using max(T2.date) > T1.date but it won't work.
Table1
myID Date
1 Jan 2002
2 Jan 2003
3 Jan 2001
Table2
myID Date
1 Jan 2002
1 Jan 2003
1 Jan 2004
2 Jan 2004
2 Jan 2005
3 Jan 2004
Wanted result
myID TBL1.Date TBL2.Date
1 Jan 2002 Jan 2004
2 Jan 2003 Jan 2005
3 Jan 2001 Jan 2004
|
|
|
|
|