说,我有一个这样的表(SQL Server 2008):
CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT)
INSERT tbl VALUES
(1, '05:00', '6:00', 1), -- will be removed
(2, '05:00', '7:00', 1), -- will be removed
(3, '05:01', '8:00', 1),
(4, '05:00', '8:00', 1),
(5, '05:00', '6:00', 2), -- will be removed
(6, '05:00', '7:00', 2),
(7, '05:00', '7:00', 3),
(8, '04:00', '7:00', 3)
我需要删除具有相同“类型”的相同“类型”(如果找到 2 个或更多)的所有记录,但具有最大“dtOut”的记录除外。换句话说,上表应该是这样的:
(3, '05:01', '8:00', 1), -- no matching 'dtIn' for 'type' = 1
(4, '05:00', '8:00', 1), -- largest 'dtOut' for 'type' = 1
(6, '05:00', '7:00', 2), -- largest 'dtOut' for 'type' = 2
(7, '05:00', '7:00', 3), -- no matching 'dtIn' for 'type' = 3
(8, '04:00', '7:00', 3) -- no matching 'dtIn' for 'type' = 4
您甚至如何从同一个表中选择多个具有相同类型的行。你不能 select * from tbl where type=type.... 无论如何,我会很感激这方面的帮助...