从表中删除共享相同ID的所有行

时间:2016-06-23 09:29:36

标签: sql tsql sybase-ase

我有下表:

Name     |   ID  |  date    |
Login    |   1   | somedate |
Command  |   1   | somedate |
Command  |   1   | somedate |
Login    |   2   | somedate |
Command  |   1   | somedate |
Command  |   2   | somedate |
Logout   |   1   | somedate |
Command  |   2   | somedate |

我想从表中删除共享相同ID的登录和注销之间的所有内容,但保留其他所有内容。 somedate字段是datetime。表中可能会有更多登录/注销,并且登录将没有相应的注销。我希望它能留在那里,因为注销最终会出现。

我在考虑使用游标。哪种方式也是性能最好的方法?决赛桌可能有几百万行。

删除后,表格应如下所示:

Name    |  ID  |  
Login   |  2   |
Command |  2   |
Command |  2   |

编辑:删除登录和注销之间的所有内容,包括具有登录/注销的行。

5 个答案:

答案 0 :(得分:2)

假设每次注销都有相应的登录信息,您可以尝试这样做:

DELETE
FROM yourTable
WHERE ID IN
(
    SELECT ID
    FROM yourTable
    WHERE Name LIKE 'Logout'
)

结果将是:

Name    |  ID  |
Login   |  2   |
Command |  2   |
Command |  2   |

如果你想让行包含"登录"和"退出"你也可以这样做:

DELETE
FROM yourTable
WHERE ID IN
(
    SELECT ID
    FROM yourTable
    WHERE Name LIKE 'Logout'
)
AND Name NOT LIKE 'Login'
AND Name NOT LIKE 'Logout'

你会得到这个结果:

Name    |  ID  |
Login   |  1   |
Login   |  2   |
Command |  2   |
Logout  |  1   |
Command |  2   |

答案 1 :(得分:2)

有一些不明确的情况(如果有没有匹配登录的命令会发生什么?如果登录是在注销的同一天我们假设登出后是登录?等等)但这应该给你一个起点;

删除所有登录或退出记录的记录,其中记录包含早期登录,更晚注销记录相同的Id;

WITH TestData AS (
    SELECT 'Login' as Name, 1 AS ID, cast('01/01/2000' as date) as Date
    UNION ALL
    SELECT 'Command' as Name, 1 AS ID, cast('02/01/2000' as date) as Date
    UNION ALL
    SELECT 'Command' as Name, 1 AS ID, cast('03/01/2000' as date) as Date
    UNION ALL
    SELECT 'Logout' as Name, 1 AS ID, cast('04/01/2000' as date) as Date
    UNION ALL
    SELECT 'Command' as Name, 1 AS ID, cast('05/01/2000' as date) as Date
    UNION ALL
    SELECT 'Command' as Name, 2 AS ID, cast('01/01/2000' as date) as Date
)
DELETE FROM TestData td1
-- Delete any records which are not login or logout
WHERE Name <> 'Login' AND Name <> 'Logout'
-- Where there is an earlier Login
AND EXISTS (SELECT 1 from TestData td2 where td2.Name = 'Login' AND td1.Id = td2.Id AND td2.Date <= td1.Date)
-- and a later logout
AND EXISTS (SELECT 1 from TestData td3 where td3.Name = 'Logout' AND td1.Id = td3.Id AND td3.Date >= td1.Date)

答案 2 :(得分:2)

让我们制作一些测试数据;

IF OBJECT_ID('tempdb..#TestData') IS NOT NULL DROP TABLE #TestData
GO
CREATE TABLE #TestData (Name varchar(7), ID int, Date datetime)
INSERT INTO #TestData (Name, ID, Date)
VALUES
 ('Login',1,'2016-06-23 12:00:00')
,('Command',1,'2016-06-23 12:05:00')
,('Command',1,'2016-06-23 12:10:00')
,('Login',2,'2016-06-23 12:15:00')
,('Command',1,'2016-06-23 12:20:00')
,('Command',2,'2016-06-23 12:25:00')
,('Logout',1,'2016-06-23 12:30:00')
,('Command',2,'2016-06-23 12:35:00')

我使用这样的查询。联接查询返回登录和注销日期时间之间的特定ID的所有行。它不会删除ID 2的任何内容,因为还没有注销该ID。

DELETE TD
FROM #TestData TD
INNER JOIN 
(
SELECT a.Name, a.ID, a.Date
FROM #TestData a
LEFT JOIN (
            SELECT ID, Date
            FROM #TestData
            WHERE Name = 'Login'
          ) lin
ON a.ID = lin.ID
LEFT JOIN (
            SELECT ID, Date
            FROM #TestData
            WHERE Name = 'Logout'
          ) lout
ON a.ID = lout.ID
WHERE a.Date BETWEEN lin.Date AND lout.Date
) b
ON TD.Date = b.Date
AND TD.ID = b.ID
AND TD.Name = b.Name

此后运行的结果将是

Name    ID  Date
Login   2   2016-06-23 12:15:00.000
Command 2   2016-06-23 12:25:00.000
Command 2   2016-06-23 12:35:00.000

编辑:更新了我的答案,现在它也删除了login / logout命令。

答案 3 :(得分:1)

我想念我的第一篇文章中的问题,编辑纠正:

delete from yourTable
where id in (select yt.Id from yourtable yt
        inner join yourtable yt2 on yt2.Id = yt.Id and yt2.Name like 'Logout'
        where yt.Name like 'Login')

以上内容应该从您的表中删除登录具有相应注销的所有内容

答案 4 :(得分:1)

select distinct(id)  into @id from your_table where Name = 'Logout'; -- will give you all user id that need to delete
delete from your_table where id in (@id );