查找列的行的最新值

时间:2013-08-23 11:36:30

标签: sql sql-server select

我有一张包含以下数据的表格。

compid  manualscandate  staticscandate  dynscandate
1       1-Aug-12    3-Aug-12    2-Aug-13
1       2-Aug-12    2-Aug-12    3-Aug-12
1       3-Aug-12    1-Aug-12    1-Aug-12
2       1-May-12    3-May-12    2-May-13
2       2-May-12    2-May-12    3-May-12
2       3-May-12    1-May-12    1-May-12

我需要找到每个compid的最新日期..这样......输出表是

compid  manualscandate  staticscandate  dynscandate
1       3-Aug-12    3-Aug-12    3-Aug-13
2       3-May-12    3-May-12    3-May-12

获得此优惠的最佳途径是什么

1 个答案:

答案 0 :(得分:3)

如果您将compid分组,那么您可以使用max()之类的聚合函数来收集您想要的数据

select compid, 
       max(manualscandate) as manualscandate, 
       max(staticscandate) as staticscandate, 
       max(dynscandate) as dynscandate
from your_table
group by compid