I have one table, assume that there are 2 users, user A and user B. User A and user B have some items; some of these items are the same but some of them are different. I want to count how many items they have common.
For example, user A and user B have 2 common items. user A and user C have only one common item. User B and user C have 2 common items, etc.
How can I do this in SQL?
Users Items
A C1
A C2
A C3
B C2
B C3
B C4
B C5
C C1
C C4
C C5
C C6
This is what I’ve tried so far:
select distinct users, count(items) over (partition by items)
from table1
Okay, this can be done with a self-join:
Here’s a SQL Fiddle to demonstrate.
The join is done on
itemas this is the column that you want to know if it’s identical. I’ve excluded where the user is the same, but you can include it if you want.The most important part is
a.username < b.username. If you don’t use this then you get the two identical rows with the username’s reversed. For instance you getA|B|2andB|A|2.I’ve also used
distinct item. If you wanted the number of items rather than the number of distinct simply remove this keyword.