2012-09-27 15 views
5

這其中有曾難倒我在過去的幾個小時,在這個階段,我想我需要一些幫助......集團通過A列,但相比B列

我需要多組從單個表比較和以確定列B中列出的項目是否匹配。例如: -

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

我想'約翰'和'大衛'返回,因爲他們在B列的項目匹配。希望這是有道理的! 在此先感謝! G

+2

什麼版本的sql服務器? – Taryn

+0

SQL Server 2008 Express – user1704276

+0

它可以非常明確地定義預期的輸出。你有上面的示例輸入。嘗試以您想要的確切格式編寫示例輸出。 –

回答

6

以下是此解決方案的SQL Fiddle,以便您可以自己玩。

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

偉大的解決方案! – RomanKonz

+0

如果你添加這些記錄: 蒂姆....蘋果 吉姆....橙色 吉姆....香蕉 它增加吉姆回報集。 – jTC

+1

@JTC謝謝。現在修好。這是你的同行評議:) – RichardTheKiwi

0

所有誰在匹配於比人更B列中的項目的人(我假設你正在尋找可能不止2場比賽?):

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與ColA2匹配:
Count(ColA1)= Count(ColA2)= Count(ColA1 x ColA2)

此方法嘗試優化查詢速度。

實現原始計數,因爲它被多次使用並可以聲明一個PK。
(CTE只是語法並被評估)

其中RA.rawcount = RB.rawcount只允許在計數相等的情況下評估連接。查詢計劃表明它首先執行。

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