Come trovo i duplicati su più colonne?


98

Quindi voglio fare qualcosa di simile a questo codice sql di seguito:

select s.id, s.name,s.city 
from stuff s
group by s.name having count(where city and name are identical) > 1

Per produrre quanto segue, (ma ignora dove corrisponde solo il nome o solo la città, deve essere su entrambe le colonne):

id      name  city   
904834  jim   London  
904835  jim   London  
90145   Fred  Paris   
90132   Fred  Paris
90133   Fred  Paris

Risposte:


137

Duplicato idper coppie namee city:

select s.id, t.* 
from [stuff] s
join (
    select name, city, count(*) as qty
    from [stuff]
    group by name, city
    having count(*) > 1
) t on s.name = t.name and s.city = t.city

Notare che se uno nameo due citycontengono null, non verranno riportati nella query esterna, ma verranno trovati nella query interna.
Adam Parkin

3
Se i valori possono eventualmente contenere null(a meno che non mi manchi qualcosa) è necessario cambiarlo in un CROSS JOIN(prodotto cartesiano completo) e quindi aggiungere una WHEREclausola come:WHERE ((s.name = t.name) OR (s.name is null and t.name is null)) AND ((s.city = t.city) OR (s.city is null and t.city is null))
Adam Parkin

55
 SELECT name, city, count(*) as qty 
 FROM stuff 
 GROUP BY name, city HAVING count(*)> 1

10

Qualcosa di simile farà il trucco. Non conosco le prestazioni, quindi esegui alcuni test.

select
  id, name, city
from
  [stuff] s
where
1 < (select count(*) from [stuff] i where i.city = s.city and i.name = s.name)

6

L'utilizzo count(*) over(partition by...)fornisce un mezzo semplice ed efficiente per individuare ripetizioni indesiderate, mentre elenca anche tutte le righe interessate e tutte le colonne desiderate:

SELECT
    t.*
FROM (
    SELECT
        s.*
      , COUNT(*) OVER (PARTITION BY s.name, s.city) AS qty
    FROM stuff s
    ) t
WHERE t.qty > 1
ORDER BY t.name, t.city

Sebbene le versioni più recenti di RDBMS supportino count(*) over(partition by...) MySQL V 8.0, sono state introdotte "funzioni finestra", come mostrato di seguito (in MySQL 8.0)

CREATE TABLE stuff(
   id   INTEGER  NOT NULL
  ,name VARCHAR(60) NOT NULL
  ,city VARCHAR(60) NOT NULL
);
INSERT INTO stuff(id,name,city) VALUES 
  (904834,'jim','London')
, (904835,'jim','London')
, (90145,'Fred','Paris')
, (90132,'Fred','Paris')
, (90133,'Fred','Paris')

, (923457,'Barney','New York') # not expected in result
;
SELECT
    t.*
FROM (
    SELECT
        s.*
      , COUNT(*) OVER (PARTITION BY s.name, s.city) AS qty
    FROM stuff s
    ) t
WHERE t.qty > 1
ORDER BY t.name, t.city
    id | nome | città | qty
-----: | : --- | : ----- | -:
 90145 | Fred | Parigi | 3
 90132 | Fred | Parigi | 3
 90133 | Fred | Parigi | 3
904834 | jim | Londra | 2
904835 | jim | Londra | 2

db <> fiddle qui

Funzioni della finestra. MySQL ora supporta le funzioni della finestra che, per ogni riga di una query, eseguono un calcolo utilizzando le righe relative a quella riga. Questi includono funzioni come RANK (), LAG () e NTILE (). Inoltre, diverse funzioni aggregate esistenti ora possono essere utilizzate come funzioni finestra; ad esempio, SUM () e AVG (). Per ulteriori informazioni, vedere la Sezione 12.21, "Funzioni della finestra" .


3

Un po 'tardi per il gioco su questo post, ma ho trovato questo modo piuttosto flessibile / efficiente

select 
    s1.id
    ,s1.name
    ,s1.city 
from 
    stuff s1
    ,stuff s2
Where
    s1.id <> s2.id
    and s1.name = s2.name
    and s1.city = s2.city

2

Devi iscriverti a qualcosa e abbinare nome e città. Quindi raggruppa per conteggio.

select 
   s.id, s.name, s.city 
from stuff s join stuff p ON (
   s.name = p.city OR s.city = p.name
)
group by s.name having count(s.name) > 1

Errore in SQL Server: tutte le colonne non aggregate devono essere in GROUP BY
gbn

0

Data una tabella di staging con 70 colonne e solo 4 che rappresentano i duplicati, questo codice restituirà le colonne offensive:

SELECT 
    COUNT(*)
    ,LTRIM(RTRIM(S.TransactionDate)) 
    ,LTRIM(RTRIM(S.TransactionTime))
    ,LTRIM(RTRIM(S.TransactionTicketNumber)) 
    ,LTRIM(RTRIM(GrossCost)) 
FROM Staging.dbo.Stage S
GROUP BY 
    LTRIM(RTRIM(S.TransactionDate)) 
    ,LTRIM(RTRIM(S.TransactionTime))
    ,LTRIM(RTRIM(S.TransactionTicketNumber)) 
    ,LTRIM(RTRIM(GrossCost)) 
HAVING COUNT(*) > 1

.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.