2012-01-09 25 views
8

Esta consultaEl uso de una cláusula HAVING en una instrucción 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 

selecciona los registros que me gustaría establecer un bit ISValid a 0.

Estos registros son registros que aparecen dos veces en mi base de datos debido a un error de entrada.

Busco algo como:

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

Este es el servidor MS SQL 2008

Gracias!

Respuesta

12

Usted puede unirse a la sub consulta de este 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

El arriba hay buenas sugerencias ... aquí hay otra manera fácil de hacerlo:

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 
) 

** Perdóneme si me equivoqué con el nombre de la columna, pero se entiende.

+0

Por lo que vale, eso irá muy despacio, ya que la subconsulta se ejecutará contra cada fila. – Eric

+2

@Eric - No creo que vaya a ser así. En qué basas eso? – JNK

+0

@JNK - Está en lo cierto: el 'en 'causa una semi unión en SQL 2008. Mi error: olvidé el motor. Perdón por la confusión :) – Eric

1

Utilice un CTE, y hacer lo que es básicamente un auto unirse a

;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 mejor aún, utilizar al sistema de ventanas.

;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 

Tenga en cuenta que el segundo no se actualiza el "primer" registro no válidos, y que se supone que hay que hay una relación de 1 a 1 entre NCAAstats y College_Translator.

Cuestiones relacionadas