对于下面给出的数据集,我想删除后面有时间戳的行。
**37C1Z2990E5E0 (TRXID) should be UNIQUE** in the below dataSet
JKLAMMSDF123 20141112 20141117 5000.0 P 1.22 RT101018 *2014-11-12 10:10:26* 37C1Z2990E5E0 101018
JKLAMMSDF123 20141110 20141114 5000.0 P 1.22 RT161002 *2014-11-12 10:11:33* 37C1Z2990E5E0 161002
-- More rows
答案 0 :(得分:1)
试试这个:
;WITH DATA AS
(
SELECT TRXID, MAX(YourTimestampColumn) AS TS
FROM YourTable
GROUP BY TRXID
HAVING COUNT(*) > 1
)
DELETE T
FROM YourTable AS T
INNER JOIN DATA AS D
ON T.TRXID = D.TRXID
AND T.YourTimestampColumn = D.TS;
答案 1 :(得分:0)
选择timestamp列的min并按所有其他列分组。
SELECT MIN(TIMESTAMP), C1, C2, C3...
FROM YOUR_TABLE
GROUP BY C1, C2, C3..
答案 2 :(得分:0)
我将使用window function
加CTE
来完成此操作。
要在删除重复项后检查结果,请使用此项。
;WITH DATA
AS (SELECT *,
Row_number()OVER(partition BY TRXID ORDER BY YourTimestampColumn) rn
FROM YourTable)
select *
FROM data
WHERE rn = 1
要delete
重复使用此内容。
;WITH DATA
AS (SELECT *,
Row_number()OVER(partition BY TRXID ORDER BY YourTimestampColumn) rn
FROM YourTable)
DELETE FROM data
WHERE rn > 1
即使您为同一TRXID