简体   繁体   中英

Counting occurrences of pairs in SQL

I have a table TAB like this:

box_id | product_id | randominfo

1      | a          | ...
1      | b          | ...
1      | c          | ...
2      | a          | ...
2      | b          | ...
3      | a          | ...
3      | c          | ...

I want for every pair of product, the number of times they are in the same box. Like this:

product 1 | product 2 | number of times

a         | b         | 2
a         | c         | 2
b         | c         | 1

I thought of something like:

SELECT p1.product_id, p2.product_id, COUNT (*) AS nb
   FROM TAB p1
      LEFT JOIN TAB p2 ON p1.product_id <> p2.product_id
      GROUP BY p1.product_id, p2.product_id
      ORDER BY nb DESC

I can't check if this works.

Your query should be fine, although you probably don't want pairs to appear twice. So use < rather than <> :

SELECT p1.product_id, p2.product_id, COUNT(*) AS nb
FROM TAB p1 JOIN
     TAB p2
     ON p1.product_id < p2.product_id
GROUP BY p1.product_id, p2.product_id
ORDER BY nb DESC;

A LEFT JOIN is unnecessary, unless you want to consider boxes that have only one product.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM