我的这张表有很多数据:
+----+-------+-------------------------+
| id | user | datetime |
+----+-------+-------------------------+
| 1 | 34534 | 2015-08-12 10:03:22.043 |
| 2 | 32423 | 2015-08-12 03:29:18.097 |
| 3 | 12312 | 2015-08-13 03:24:10.073 |
| 4 | 34232 | 2015-08-13 03:24:10.073 |
| 5 | 32462 | 2015-08-13 03:24:10.073 |
| 6 | 45354 | 2015-08-14 04:12:04.023 |
+----+-------+-------------------------+
我想在完全相同的行的日期时间之间创建一分钟的差距。与上述情况一样,行号3,4,5。这三个日期时间间隔一分钟。
答案 0 :(得分:2)
试试这个
select id,
user,
[datetime] = case when rn = 1 then [datetime] else dateadd(minute,rn-1,[datetime]) END
from
(
select row_number()over(partition by [datetime] order by id) rn,*
from yourtable
) A
要更新表格,请使用此
with cte As
(
select row_number()over(partition by [datetime] order by id) rn,*
from yourtable
)
Update CTE set [datetime] = case when rn = 1 then [datetime] else dateadd(minute,rn-1,[datetime]) END
答案 1 :(得分:0)
While exists (Select * from table a
join table b
on b.Id > a.Id
and b.datetime = a.DateTime)
Update t set
datetime = DateAdd(minute, 1, DateTime)
From table t
where Exists (Select * from table
Where id > t.Id
and DateTime = t.DateTime)