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 |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-10-12 : 03:53:18
|
| i have a tablel ike this :identity column,accout number,branch,bank,statusidand there can be an account with all the same deteils apear more then once widht diffrent statuses!for example :(1),12345,12,3,1(15000)12345,12,3,3(60001),12345,12,3,4(60333),12345,12,3,0(61234),12345,12,3,3and i want to make a query that will get me te last bank account (where last is by the heighest (identity column) where its statusid for example=3 but!!! it must be in the most top id!if in this example i wanted status 0 of the above account i whouldnt get anything beacuse 0 is not in the most top(id)how can i do this?thnaks in advancepelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-12 : 03:57:00
|
quote: it must be in the most top id!
What do you mean by that? Could you explain a bit?Also you haven't provided column names..Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 03:59:16
|
| select t.*from mytable tinner join (select max(idcol) maid, account from mytable where status = 3 group by account) don d.maid = t.idcol and d.account = t.accountPeter LarssonHelsingborg, Sweden |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-10-12 : 04:15:58
|
| yes i did this are the coklumns names :identity column,accout number,branch,bank,statusidIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-10-12 : 04:25:50
|
Peso, I don't think that's quite what he's after. That's going to return a record even if there are more recent entries of different statuses. I think what he wants is something like this:-- for account=12345 and status=3select t.*from mytable tinner join (select max(idcol) maid, account from mytable group by account) don d.maid = t.idcol and d.account = t.accountwhere t.account=12345and t.status=3 Mark |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 04:32:59
|
| You are right.Delete the WHERE t.Status = 3 in the derived table and add it after the INNER JOIN in my code.Peter LarssonHelsingborg, Sweden |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-10-12 : 04:41:20
|
| i did so and it gave the corect result!thnaks alotIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
|
|
|
|
|