Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
zaidrahman
Starting Member
5 Posts |
Posted - 2008-06-05 : 12:54:01
|
Hello All,Can someone help me with this query?I have the following table and data structureTable1: OrdersField1: OrderIDTable2: OrderStatusField1: OrderStatusIDFIeld2: OrderIDField3: StatusIDField4: InsertDateSample Data:Table1:1112Table2:1,11,1,6/1/20082,12,1,6/1/20083,11,2,6/2/20084,12,2,6/2/20085,11,3,6/3/20086,11,4,6/4/2008Wanted Results:11,4,6/4/200812,2,6/2/2008 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-05 : 13:38:58
|
trySelect t1.col1, max(t2.col1), max(t2.datecol) from table1 t1 inner join table2 t2 on t1.keycol=t2.keycolgroup by t1.col1MadhivananFailing to plan is Planning to fail |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-05 : 13:43:58
|
Not sure if OP meant this?SELECT t1.OrderID,t2.StatusID,t2.InsertDateFROM Table1 t1INNER JOIN Table2 t2ON t2.OrderID=t1.OrderIDINNER JOIN (SELECT OrderID,MAX(InsertDate) as MaxDate FROM Table2 GROUP BY OrderID)t3ON t3.MaxDate=t2.InsertDateAND t3.OrderID=t2.OrderID |
 |
|
zaidrahman
Starting Member
5 Posts |
Posted - 2008-06-05 : 13:58:10
|
Thank you very much. Works like a champ. |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-06-05 : 16:21:48
|
Use visakh's code, please. I don't think madhivanan's is what you are looking for.e4 d5 xd5 Nf6 |
 |
|
|
|
|
|
|