Hi,Sometimes I need to retrieve values from a table where they match the max() value from a different table. In this example, I'm getting the max version number from #process, then joining it back on itself to get the processIDs that I need. I'm then joining this to the #items table to return the results I wantThe method I use is fine, but I just wondered if there's a better / simpler / more efficient way of doing it.-- prepare sample datacreate table #Process (ProcessID int, ProcessName char(20), version int)create table #Items (ItemID int, ProcessID int, ItemName char(20))insert #Processselect 101, 'Process1', 1 union allselect 102, 'Process2', 1 union allselect 103, 'Process3', 1 union allselect 104, 'Process1', 2 union allselect 105, 'Process1', 3insert #Itemsselect 900, 101, 'Item1' union allselect 901, 101, 'Item2' union allselect 902, 102, 'Item3' union allselect 903, 103, 'Item4' union allselect 904, 101, 'Item5' union allselect 905, 102, 'Item6' union allselect 906, 103, 'Item7' union allselect 907, 104, 'Item8' union allselect 908, 103, 'Item9' union allselect 909, 102, 'Item10' union allselect 910, 103, 'Item11' union allselect 911, 105, 'Item12' union allselect 912, 105, 'Item13' select itemid, itemname from #itemsinner join--get the latest process info (select processid from #Process inner join (select processname, max(version) as version from #process group by processname) as maxvalues on #Process.processname = maxvalues.processname and #Process.version = maxvalues.version) as latestprocesseson #items.processid = latestprocesses.processiddrop table #processdrop table #items
Cheers,Yonabout