如图,请教sqlserver选择语句。

图片说明
其中两个id是混合主键,要求选择出红框中的部分(就是如果前面两项数据重复,就要求选择出historyid中较大的那一个,忽略较小的那个)。

select max(historyworkitemid) from table group by iworkitemid就可以了,先分组,每个分组中取最大的。

select * from
(select iworkitemid, max(historyworkitemid) mid from table group by iworkitemid) newtab , table
where newtab.mid=table.historyworkitemid and newtab.iworkitemid=table.iworkitemid;