用于查找具有相同值集的行的SQL查询

时间:2014-11-29 19:11:21

标签: sql sql-server database tsql

假设我有以下表格

Customer_ID  Item_ID
1            A
1            B
2            A
2            B
3            A

我需要的是找到购买同一套物品的顾客 输出:

Customer_id  Customer_id
id1          id2

(id1< id2)

如果有两个以上的客户拥有相同的项目集,例如三个,那么输出必须是下一个:

Customer_id  Customer_id
id1          id2
id2          id3
id1          id3

由于

5 个答案:

答案 0 :(得分:1)

在你的情况下:

create table CustomerAA (
    Customer_Id int ,
    ItemId varchar(10)
    );

Insert Into CustomerAA(Customer_Id,ItemId) values(1,'A')
Insert Into CustomerAA(Customer_Id,ItemId) values(1,'B')
Insert Into CustomerAA(Customer_Id,ItemId) values(2,'A')
Insert Into CustomerAA(Customer_Id,ItemId) values(2,'B')
Insert Into CustomerAA(Customer_Id,ItemId) values(3,'A')

您可以像上面一样使用Left Join

Select Distinct C1.Customer_Id , C2.Customer_Id
From CustomerAA As C1
Left Join CustomerAA As C2 on ( C1.ItemId = C2.ItemId )
Where
    ( C1.Customer_Id < C2.Customer_Id )

或者您可以使用Inner Join

Select Distinct C1.Customer_Id , C2.Customer_Id
From CustomerAA As C1
Inner Join CustomerAA As C2 on ( C1.ItemId = C2.ItemId )
Where
    ( C1.Customer_Id < C2.Customer_Id )

或者

Select Distinct C1.Customer_Id , C2.Customer_Id
From CustomerAA As C1
Inner Join CustomerAA As C2 
    on ( 
           ( C1.ItemId = C2.ItemId ) 
           And 
           ( C1.Customer_Id < C2.Customer_Id ) 
       )

答案 1 :(得分:1)

这个中间有一个enumID但是同样的东西

with cte as ( SELECT [sID], [enumID], [valueID], count(*) over (partition by [sID], [enumID]) as ccount
                FROM [docMVenum1] 
               WHERE [sID] < 10000 ) 
select [cte1].[sID], [cte2].[sID], [cte1].[enumID] -- , [cte1].[valueID], [cte1].[ccount]
  from cte as [cte1] 
  join cte as [cte2] 
    on [cte1].[sID] < [cte2].[sID] 
   and [cte1].[enumID]  = [cte2].[enumID]
   and [cte1].[valueID] = [cte2].[valueID]
   and [cte1].[ccount]  = [cte2].[ccount]
 group by [cte1].[sID], [cte2].[sID], [cte1].[enumID], [cte1].[ccount]
having count(*) =  [cte1].[ccount]
 order by [cte1].[sID], [cte2].[sID] --, [cte1].[enumID], [cte1].[valueID];

答案 2 :(得分:0)

Select C1.Customer_ID , C2.Customer_ID 
from table C1 
inner join table C2 on C1.Customer_ID < C2.Customer_ID and C1.Item_ID =Item_ID

答案 3 :(得分:0)

试试这个。我们的想法是利用外连接和count忽略null

的事实
select t1.customer_id, t2.customer_id
from tbl t1
left join tbl t2
on t1.customer_id < t2.customer_id
and t1.item_id = t2.item_id
group by t1.customer_id, t2.customer_id
having count(t1.item_id) = count(t2.item_id)

答案 4 :(得分:0)

我找到了解决问题的方法 首先,我创建了临时表。该计数器具有包含每个客户购买的不同项目的列。 它看起来像这样:

Customer_ID Item_Id num
1           A       2
1           B       2 
2           A       2
2           B       2
3           A       1

下一个查询

DECLARE @tmp TABLE 
(
    Customer_ID int,
    Item_ID nvarchar(max),
    num int
)

INSERT INTO @tmp 
SELECT tbl.Customer_Id, tbl.ItemId, A.num
FROM tbl
INNER JOIN 
    (SELECT Customer_Id,  COUNT(DISTINCT ItemId) AS num
    FROM tbl 
    GROUP BY Customer_Id) A 
ON tbl.Customer_Id = A.Customer_Id

之后,我使用下一个查询

SELECT t1.Customer_Id, t2.Customer_Id
FROM @tmp t1
INNER JOIN @tmp t2
ON t1.Customer_Id < t2.Customer_Id AND t1.Item_ID = t2.Item_ID
GROUP BY t1.Customer_Id, t1.num, t2.Customer_Id, t2.num 
HAVING COUNT(t1.Item_ID) = COUNT(t2.Item_ID) AND
    t1.num = COUNT(t1.Item_ID) AND
    t2.num = COUNT(t2.Item_ID)

所以结果将是

Customer_ID Customer_ID 
1           2