5

在过去的几个小时里,这个让我难过,在这个阶段我想我需要一些帮助......

我需要比较单个表中的多个组,并确定列 B 中列出的项目匹配的位置。例如: -

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

我希望返回“John”和“David”,因为它们在 col B 中的项目匹配。希望这是有道理的!提前致谢!G

4

3 回答 3

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
于 2012-09-27T19:43:16.320 回答
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
于 2012-09-27T21:04:22.447 回答
0

ColA1 与 ColA2 匹配,如果:
计数 (ColA1) = 计数 (ColA2) = 计数 (ColA1 x ColA2)

这种方法试图优化查询速度。

物化原始计数,因为它被多次使用并且可以声明一个 PK。
(CTE 只是语法并被评估)

where 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 
于 2012-09-28T01:35:18.397 回答