Possible to do a delete with a HAVING clause?

前端 未结 5 1525
野性不改
野性不改 2020-12-06 04:02

I want to do something like below:

DELETE UserPredictions
  GROUP BY UserId
  HAVING COUNT(*) < 500

But I\'m getting a syntax error. Is

5条回答
  •  醉话见心
    2020-12-06 04:44

    You can do so much with using row_number() OVER (partition by ) and CTE's, but only if your RDBMS supports it.

    Example:

    WITH CTE AS 
    (
        SELECT UserId,
        ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY UserId) AS rowcount    
        FROM UserPredictions
    )
    DELETE FROM CTE
    WHERE rowcount < 500
    

    Additional info about the row count function:

    https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-2017

提交回复
热议问题