Table 1:
id | date | value
1 15/04/2013 15:45 1
1 15/04/2013 15:55 1
2 16/04/2013 15:45 2
2 16/04/2013 15:55 2
3 17/04/2013 15:25 3
3 17/04/2013 15:35 3
使用查询后
Select *
from (
Select *, ROW_NUMBER() OVER (Partition by id, date ORDER BY date DESC) from table X
) A
除了在同一天但不同时间出现的行外,它正确排列所有行。
换句话说,我想编写一个可以在日期和时间上排名的sql。
答案 0 :(得分:1)
你在找这个吗?
SELECT a.*
FROM (
SELECT x.*, ROW_NUMBER() OVER (PARTITION BY id, DATEADD(dd, 0, DATEDIFF(dd, 0, date)) ORDER BY date DESC) rownum
FROM table1 x
) A
示例输出:
| ID | DATE | VALUE | ROWNUM | ------------------------------------------------------ | 1 | April, 15 2013 15:55:00+0000 | 1 | 1 | | 1 | April, 15 2013 15:45:00+0000 | 1 | 2 | | 2 | April, 16 2013 15:55:00+0000 | 2 | 1 | | 2 | April, 16 2013 15:45:00+0000 | 2 | 2 | | 3 | April, 17 2013 15:35:00+0000 | 3 | 1 | | 3 | April, 17 2013 15:25:00+0000 | 3 | 2 |
这是 SQLFiddle 演示
答案 1 :(得分:0)
试
order by convert(datetime, date, 103) DESC