日期:2014-05-18  浏览次数:20601 次

求 一条 很难的sql语句
有两张表 workflow(工单表)workflowhistory(工单流转历史表) id号唯一 ,每条工单的flownumber 从1开始递增。

table workflow 
id name
00 content
01 content

table workflowhistory

id flownumber dealtime
00 1 2007-10-9 20:00:00
00 2 2007-10-10 20:00:00
01 1 2007-10-9 20:00:00
01 2 2007-10-10 20:00:00

求sql语句 把workflow workflowhistory 通过id连接起来,并且每个id只保留时间最晚的记录(或者说是flownuber最大的那条记录)
ru
id flownumber dealtime
00 2 2007-10-10 20:00:00
01 2 2007-10-10 20:00:00


------解决方案--------------------
select * from (select * from workflowhistory 
where flownumber in (select max(flownumber) from workflowhistory group by id)) a join workflow on workflow.id= a.id