8

我需要从具有相同和. 如何以最少数量的 sql 语句实现这一目标。Logcustomer_iddeactivation_date

我正在使用 MS SQL Server 2008 速成版。

4

3 回答 3

16

就像是:

DELETE FROM Log WHERE LogId NOT IN 
 (SELECT Min(LogId) FROM Log GROUP BY customer_id, deactivation_date)
于 2012-04-27T07:29:42.103 回答
14

或者:

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;
于 2012-04-27T07:32:38.887 回答
2

你没有给我们太多的工作。但也许是这样的:

;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 
    )
于 2012-04-27T07:28:08.983 回答