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 |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-08-09 : 09:31:55
|
sankaran writes "i have a table containing itemname, date & location. itemname repeats with different location and date values. i need to extract distinct items with their location on latest date. any help?thnks & rgdsk.s.unni." |
|
Q
Yak Posting Veteran
76 Posts |
Posted - 2006-08-09 : 09:37:20
|
select t1.itemname, t1.location, t1.datefrom table t1where date =(select max(t2.date) from table t2 where t2.itemname = t1.itemname) |
 |
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-08-09 : 09:42:46
|
[code]SELECT mt.ItemName, mt.[Date], mt.LocationFROM MyTable AS mtJOIN (SELECT mt.ItemName, MAX(mt.[Date]) AS [Date] FROM MyTable AS mt GROUP BY mt.ItemName ) AS MaxDateON mt.ItemName = MaxDate.ItemName AND mt.[Date] = MaxDate.[Date][/code]Mark |
 |
|
Q
Yak Posting Veteran
76 Posts |
Posted - 2006-08-09 : 09:47:38
|
What should happen when there are 2 records as follows:1. itemA, 2006-8-9, Loc12. itemB, 2006-8-9, Loc22. itemB, 2006-8-9, Loc2 Which of these records is the latest??? Maybe you need some more data. Or maybe this is not possible in your case... |
 |
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-08-09 : 09:56:42
|
Hi Q, I'm assuming that those two rows are both meant to have the same itemname...Mark |
 |
|
Q
Yak Posting Veteran
76 Posts |
Posted - 2006-08-09 : 09:59:28
|
Yes, you're right... |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-09 : 10:00:04
|
sankaran, post some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|