-1

如何为每个 [DetailId] 选择具有更大 [CreateDate] 的 [FlowId] 例如详细信息数据 在此处输入图像描述

4

3 回答 3

1

尝试这个

select * from t a
where dt = (select max(dt)
            from T b where a.detailid = b.detailid)
order by 1

SQL 演示

于 2013-09-28T12:07:07.367 回答
0

假设 SQL Server 2005 及更高版本,请使用ROW_NUMBER 之类的排名函数:

with TopFlow as
(
  select *
    , FlowRank = row_number() over (partition by DetailID
                                   order by [Date] desc, FlowID desc)
  from MyTable
)
select DetailID
  , FlowID
  , [Date]
from TopFlow
where FlowRank = 1

SQL Fiddle 与演示

于 2013-09-28T11:59:15.037 回答
0
select M.*
from MyTable M
inner join 
(
select DetailID,max(Date) Date
from MyTable
Group by DetailID
)T
on M.DetailID=T.DetailID
and M.Date=T.Date
order by M.DetailID

SQL小提琴

于 2013-09-28T12:04:17.600 回答