删除重复项后面的行
我有一个用户登录和注销标记的列表。不幸的是,登录条目后面可能并不总是跟着注销条目。
我希望删除按 [event_date] 排序时与上一行具有相同 [event] 和 [user_id] 的任何行 关于如何执行此操作有什么建议吗?
示例表
CREATE TABLE #LOG (
[id] int IDENTITY(1,1),
[user_id] int,
[event] varchar(50),
[event_date] datetime
);
INSERT INTO #LOG ([user_id], [event], [event_date])
SELECT 1,'LOGIN',{ts '2010-12-15 15:31:59'}
UNION ALL SELECT 1,'LOGOUT',{ts '2010-12-15 15:32:55'}
UNION ALL SELECT 1,'LOGIN',{ts '2010-12-15 15:38:04'}
UNION ALL SELECT 1,'LOGOUT',{ts '2010-12-15 15:38:17'}
UNION ALL SELECT 1,'LOGOUT',{ts '2010-12-15 15:38:45'} -- Delete
UNION ALL SELECT 2,'LOGIN',{ts '2010-12-15 16:59:39'}
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:00:08'}
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:00:39'} -- Delete
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:01:16'} -- Delete
UNION ALL SELECT 2,'LOGIN',{ts '2010-12-15 17:01:38'}
UNION ALL SELECT 2,'LOGIN',{ts '2010-12-15 17:02:26'} -- Delete
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:02:39'}
I have a list of user login and logout stamps. Unfortunately a LOGIN entry might not always be followed by a LOGOUT entry.
I wish to delete any row which has the same [event] and [user_id] as previous row when ordered by [event_date]
Any suggestions on how to do this?
Example table
CREATE TABLE #LOG (
[id] int IDENTITY(1,1),
[user_id] int,
[event] varchar(50),
[event_date] datetime
);
INSERT INTO #LOG ([user_id], [event], [event_date])
SELECT 1,'LOGIN',{ts '2010-12-15 15:31:59'}
UNION ALL SELECT 1,'LOGOUT',{ts '2010-12-15 15:32:55'}
UNION ALL SELECT 1,'LOGIN',{ts '2010-12-15 15:38:04'}
UNION ALL SELECT 1,'LOGOUT',{ts '2010-12-15 15:38:17'}
UNION ALL SELECT 1,'LOGOUT',{ts '2010-12-15 15:38:45'} -- Delete
UNION ALL SELECT 2,'LOGIN',{ts '2010-12-15 16:59:39'}
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:00:08'}
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:00:39'} -- Delete
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:01:16'} -- Delete
UNION ALL SELECT 2,'LOGIN',{ts '2010-12-15 17:01:38'}
UNION ALL SELECT 2,'LOGIN',{ts '2010-12-15 17:02:26'} -- Delete
UNION ALL SELECT 2,'LOGOUT',{ts '2010-12-15 17:02:39'}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 SQL Server 的 ROW_NUMBER 功能将是一个选项
SQL声明
Using the ROW_NUMBER functionality of SQL Server would be an option
SQL Statement
我的理解是,您想要删除条目,使模式始终为
In,Out,In,Out,etc
。这意味着如果前面的记录(按 user_id,然后是 event_date 排序)属于同一事件,则记录将被删除。
我可以使用两个选项来解决这个问题...
或者...
无论哪种方式,我强烈推荐一个覆盖
user_id
然后event_date
的索引。注意: 第一个版本不处理两个事件具有相同时间戳的可能性。然而,后者确实如此。
My understanding is that you want to delete entries such that the pattern is always
In,Out,In,Out,etc
.This means a record is deleted if the preceding record (when order by user_id, then event_date), is of the same event.
There are two options I'd use to go about this...
Or...
Either way, I highly recommend an Index covering
user_id
thenevent_date
.Note: The first version does not cope with the possibility of two events having the same timestamp. The latter, however, does.
如果必须删除重复的行。那么就不需要设置order by子句了。
尝试下面
If you have to delete duplicate row. Then no need to set the order by clause.
Try below