如下表,結束時間TimeEnd相同的行,只保留開始時間TimeStart最近的一行?
EmpNO | Name | Date | TimeStart | TimeEnd | Mins |
1001 | TEST | 2013/7/9 | 11:20:46 | 11:45:22 | 25 |
1001 | TEST | 2013/7/9 | 11:44:06 | 11:45:22 | 1 |
1001 | TEST | 2013/7/9 | 07:30:55 | 11:45:22 | 255 |
1001 | TEST | 2013/7/9 | 11:20:46 | 17:49:55 | 389 |
1001 | TEST | 2013/7/9 | 11:44:06 | 17:49:55 | 365 |
SELECT * FROM table1 a WHERE 1>(SELECT COUNT(*) FROM table1 b WHERE a.TimeEnd=b.TimeEnd AND b.TimeStart>a.TimeStart)
SELECT * FROM (select *,row=row_number() over (PARTITION BY timeEnd ORDER BY timestart DESC) FROM Tmp AS t where t.row=1
感觉这种方式更容易理解
@mifeng2012: 呵呵~
select b.* from tableName as b inner join
(select a.empno,a.name,a.date,a.timeend,min(a.Mins) as t1 from tableName as a group by a.empno,a.name,a.date,a.timeend) on a.t1 = b.Mins
按照TimeEnd字段分组,然后按TimeStart逆序排序。
SELECT * FROM 表名 group by TimeEnd order by TimeStart DESC