5

我有一个用户登录和注销戳列表。不幸的是,LOGIN 条目可能并不总是后跟 LOGOUT 条目。当[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'}
4

4 回答 4

3
;WITH T1 AS
(
SELECT * , 
        ROW_NUMBER() OVER (ORDER BY event_date)-
        ROW_NUMBER() OVER (PARTITION BY [user_id], [event] 
                               ORDER BY event_date) AS Grp
FROM #LOG
),T2 AS
(
SELECT 
   ROW_NUMBER() OVER (PARTITION BY [user_id], [event], Grp 
                          ORDER BY event_date) RN
FROM T1
)
DELETE FROM T2 
WHERE RN > 1
于 2011-05-25T11:43:24.497 回答
2

我的理解是您要删除条目,以使模式始终为In,Out,In,Out,etc.

这意味着如果前面的记录(按 user_id,然后按 event_date 排序)属于同一事件,则删除一条记录。

我有两个选择来解决这个问题......

DELETE
  #log
WHERE
  event = (
           SELECT
             TOP 1
             event
           FROM
             #log AS [preceding]
           WHERE
             [preceding].user_id = #log.user_id
             AND [preceding].event_date < #log.event_date
           ORDER BY
             [preceding].event_date DESC
          )

或者...

WITH ordered_log AS (
  SELECT
    ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_date) AS user_event_id,
    *
  FROM
    #log
)

DELETE
  ordered_log
FROM
  ordered_log
INNER JOIN
  ordered_log   AS [preceding]
    ON  [preceding].login_id      = [ordered_log].login_id
    AND [preceding].user_event_id = [ordered_log].user_event_id - 1
WHERE
  [preceding].event = [ordered_log].event

无论哪种方式,我强烈推荐一个索引覆盖user_idthen event_date


注意:第一个版本不能处理两个事件具有相同时间戳的可能性。然而,后者确实如此。

于 2011-05-25T11:19:48.280 回答
2

可以选择使用SQL Server的ROW_NUMBER功能

SQL 语句

;WITH q AS (
    SELECT  Rownumber = ROW_NUMBER() OVER (ORDER BY user_id, event_date)
    , user_id
    , event
    , event_date
    FROM    #LOG
)
DELETE FROM #LOG
FROM    #LOG l
        INNER JOIN (
            SELECT  q2.*
            FROM    q q1
                    INNER JOIN q q2 ON  q2.Rownumber = q1.Rownumber + 1
                                        AND q2.user_id = q1.user_id
                                        AND q2.event = q1.event
        ) q ON  q.user_id = l.user_id
                AND q.event_date = l.event_date

SELECT  *
FROM    #LOG
于 2011-05-25T11:23:57.640 回答
0

如果您必须删除重复的行。则无需设置 order by 子句。

试试下面

Delete l from #LOG l
Inner Join 
(
    Select id from #LOG l
    Inner Join(
        Select user_id, event from #LOG 
        group by user_id, event
        having COUNT(user_id) > 1 and COUNT(event) > 1
    )T
    on (l.user_id = t.user_id) and (l.event = t.event)
)T
on T.id = l.id
于 2011-05-25T12:02:50.020 回答