在UPDATE语句中使用HAVING子句

Tyl*_*itt 10 sql having sql-server-2008

这个查询

SELECT
FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, StatTypeId, COUNT(*) AS 'Count'
FROM NCAAstats
INNER JOIN College_Translator
ON College_Translator.AccountID = NCAAstats.AccountId
GROUP BY FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, CalendarYear, StatTypeId
HAVING COUNT(*) >1
ORDER BY 'Count' DESC
Run Code Online (Sandbox Code Playgroud)

选择我想一个记录集ISValid0.

这些记录是由于输入错误而在我的数据库中出现两次的记录.

我正在寻找类似的东西:

UPDATE NCAAstats
SET IsValid = 0
WHERE (my select statement)
Run Code Online (Sandbox Code Playgroud)

这是在MS SQL SERVER 2008上

谢谢!

Eri*_*ric 17

您可以像这样加入该子查询:

update n1 set
    isvalid = 0
from
    ncaastats n1
    inner join (
        SELECT
        FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, StatTypeId, COUNT(*) AS 'Count'
        FROM NCAAstats
        INNER JOIN College_Translator
        ON College_Translator.AccountID = NCAAstats.AccountId
        GROUP BY FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, CalendarYear, StatTypeId
        HAVING COUNT(*) >1
    ) n2 on
        n1.accountid = n2.accountid
Run Code Online (Sandbox Code Playgroud)