2012-01-09 12 views
8

Questa queryUtilizzo di una clausola HAVING in un'istruzione UPDATE

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 

seleziona i record che vorrei per impostare un ISValid bit 0.

Questi record sono record visualizzati due volte nel mio database a causa di un errore di input.

Sto cercando qualcosa di simile:

UPDATE NCAAstats 
SET IsValid = 0 
WHERE (my select statement) 

Questo è su MS SQL Server 2008

Grazie!

risposta

12

Puoi partecipare a quella subquery in questo modo:

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 
2

Il sopra ci sono dei buoni suggerimenti .... ecco un altro modo semplice per farlo:

update ncaastats set isvalid = 0 
where accountId in (
    SELECT AccountId 
    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 
) 

** Perdonami se ho incasinato il nome delle colonne, ma hai capito l'idea.

+0

Per quello che vale, andrà molto lentamente, poiché la subquery verrà eseguita su ogni riga. – Eric

+2

@Eric - Non penso che lo farà. Su cosa stai basandoti? – JNK

+0

@JNK - Sei corretto - il 'in' causa un semi join in SQL 2008. La mia colpa - ha dimenticato il motore. Ci scusiamo per la confusione :) – Eric

1

Utilizzare un CTE, e fare ciò che è fondamentalmente un self join

;with NCAAstatsToUpdate(
    SELECT AccountId 
    FROM NCAAstats n 
     INNER JOIN College_Translator ct 
     ON ct.AccountID = n.AccountId 
    GROUP BY FirstName, LastName, n.AccountId, ct.school_name, 
     CalendarYear, StatTypeId 
    HAVING COUNT(*) >1) 
UPDATE NCAAstats 
SET IsValid=0 
FROM NCAAstats n 
inner join NCAAstatsToUpdate u 
    on n.AccountId = u.AccountId 

O meglio ancora, utilizzare le funzioni di windowing.

;with NCStats as(
Select distinct row_number() over (partition by FirstName, LastName, n.AccountId, ct.school_name, 
     CalendarYear, StatTypeId order by n.accountId) rw, n.* 
FROM NCAAstats n 
     INNER JOIN College_Translator ct 
     ON ct.AccountID = n.AccountId 
) 
Update NCStats 
Set IsValid=0 
Where rw>1 

noti che secondo non aggiorna la "prima" record disattivo e che presuppone l'esistenza che esiste un rapporto 1 a 1 tra NCAAstats e College_Translator.

Problemi correlati