2012-09-27 14 views
5

Dieser hat mich in den letzten paar Stunden und in diesem Stadium stapfte hatte ich glaube, ich brauche Hilfe ...Gruppe von Spalte A, aber vergleichen Spalte B

Ich brauche mehrere Gruppen aus einer einzigen Tabelle zu vergleichen und um zu ermitteln, wo die in Spalte B aufgeführten Elemente übereinstimmen. Zum Beispiel: -

Col A...............Col B 
John................Apple 
John................Orange 
John................Banana 
Mary................Orange 
Mary................Strawberry 
David...............Apple 
David...............Orange 
David...............Banana 

Ich möchte 'John' und 'David' zurückgegeben, weil ihre Elemente in Spalte B übereinstimmen. Hoffe, das macht Sinn! Vielen Dank im Voraus! G

+2

welche Version von SQL-Server? – Taryn

+0

SQL Server 2008 Express – user1704276

+0

Es hilft, die Ausgabe sehr explizit zu erwarten. Sie haben oben eine Beispieleingabe. Versuchen Sie, die Beispielausgabe in genau dem Format zu schreiben, das Sie möchten. –

Antwort

6

Hier ist die SQL Fiddle für diese Lösung, damit Sie selbst damit spielen können.

select A.ColA Person1, B.ColA Person2 
    from (select ColA, count(ColB) CountBs 
      from tbl 
      group by ColA) G1 
    join (select ColA, count(ColB) CountBs 
      from tbl 
      group by ColA) G2 on G1.ColA < G2.ColA 
          and G1.CountBs = G2.CountBs 
    join tbl A on A.ColA = G1.ColA 
    join tbl B on B.ColA = G2.ColA and A.ColB = B.ColB 
group by A.ColA, B.ColA, G1.CountBs 
having count(distinct A.ColB) = G1.CountBs 

-- subqueries G1 and G2 are the same and count the expected colB's per colA 
-- G1 and G2 are joined together to get the candidate matches 
-- of ColA with the same number of ColB's 
-- we then use G1 and G2 to join into tbl, and further join 
-- between A and B where the ColB's match 
-- finally, we count the matches between A and B and make sure the counts match 
-- the expected count of B's for the pairing 
+0

großartige Lösung! – RomanKonz

+0

Wenn Sie diese Datensätze hinzufügen: Tim .... Apple Jim .... Orange Jim .... Banana Es fügt Jim zu den Return-Set. – jTC

+1

@ JTC Danke. jetzt behoben. Das ist Peer-Review für Sie :) – RichardTheKiwi

0

alle Menschen, die auf ein Element in Spalte b haben, die mehr als auf Person abgestimmt ist (ich nehme an, Sie für möglicherweise mehr als nur 2 Ursachen suchen?):

SELECT tableName.ColA, tableName.ColB 
FROM (SELECT ColB 
    FROM tableName 
    GROUP BY ColB 
    HAVING COUNT(1) > 1) fruits 
INNER JOIN tableName ON fruits.ColB = tableName.ColB 
ORDER BY tableName.ColB, tableName.ColA 
0

ColA1 stimmt mit ColA2 überein, wenn:
Count (ColA1) = Anzahl (ColA2) = Anzahl (ColA1 x ColA2)

Dieser Ansatz versucht, Abfrage-Geschwindigkeit zu optimieren.

Verarbeiten Sie die rohe Anzahl, da sie mehr als einmal verwendet wird und eine PK deklarieren kann.
(ein CTE ist nur Syntax und wird ausgewertet)

Die where RA.rawcount = RB.rawcount ermöglicht nur den Join auszuwerten, wenn die counts gleich sind. Und der Abfrageplan zeigt an, dass er zuerst ausgeführt wird.

create table #rawcount 
(ColA varchar(50) not null primary key, rawcount int not null) 
insert into #rawcount 
select [ColA], COUNT(*) as [rawCount] 
from  [tbl] 
group by [ColA] 
order by [ColA] 

select a.ColA as ColA1, b.ColA as ColA2, COUNT(*) [matchcount] 
from tbl A 
join tbl B 
on a.ColB = b.ColB 
and a.ColA < b.ColA 
join #rawcount RA 
on RA.ColA = A.ColA 
join #rawcount RB 
on RB.ColA = B.ColA 
where RA.rawcount = RB.rawcount -- only evaluate if count same 
group by a.ColA, b.ColA, RA.rawcount 
having COUNT(*) = RA.rawcount 
Verwandte Themen