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 |
|
sqlteamuser
Starting Member
2 Posts |
Posted - 2003-08-01 : 14:57:53
|
| Hi,I have a little problem with a Max unique part query.I am trying to get result that shows all fields for each unique part the most recent dateIn my parts table I have:Date_time, part_num, cost, district2003-07-20 10:10:00, part1, 10, 12003-07-20 10:11:00, part1, 20, 22003-07-20 10:11:00, part2, 30, 22003-07-20 10:11:00, part3, 20, 2I tried:Select max(Date_Time), part, cost, district From Data_PartGroup By Date_Time , part, cost, districtShows all of the columnsWhat is the query to show all fields for each unique part the most recent date? That would show:2003-07-20 10:11:00, part1, 20, 22003-07-20 10:11:00, part2, 30, 22003-07-20 10:11:00, part3, 20, 2Thanks in advance, |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2003-08-01 : 15:23:58
|
You can try thisSELECT a.*FROM Data_Part a join ( SELECT MAX(Date_Time)'MaxDate', part FROM Data_Part GROUP BY part ) b ON a.part = b.part AND a.date_time = b.MaxDate but trying this using datetime is yucky.... |
 |
|
|
sqlteamuser
Starting Member
2 Posts |
Posted - 2003-08-01 : 16:19:01
|
| Thanks alot,That worked.I just wanted to find out how did you know that you needed to use a subquery.Thanks again, |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2003-08-07 : 08:36:55
|
| Years of trial and error...trial and error...and even more trial and error.I have also read some T-SQL books and visit this site often. |
 |
|
|
|
|
|
|
|