我需要从具有相同和. 如何以最少数量的 sql 语句实现这一目标。Log
customer_id
deactivation_date
我正在使用 MS SQL Server 2008 速成版。
我需要从具有相同和. 如何以最少数量的 sql 语句实现这一目标。Log
customer_id
deactivation_date
我正在使用 MS SQL Server 2008 速成版。
就像是:
DELETE FROM Log WHERE LogId NOT IN
(SELECT Min(LogId) FROM Log GROUP BY customer_id, deactivation_date)
或者:
with cte as (
select row_number() over (partition by customer_id, deactivation_date
order by log_id) as rn
from Log)
delete from cte
where rn > 1;
你没有给我们太多的工作。但也许是这样的:
;WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY [Log].customer_id ORDER BY
deactivation_date) AS RowNbr,
[Log].*
FROM
[Log]
)
DELETE FROM [Log]
WHERE EXISTS
(
SELECT
NULL
FROM
CTE
WHERE
CTE.RowNbr>1
AND CTE.log_id =[Log].log_id
)