Untitled

 avatar
unknown
plain_text
9 months ago
574 B
13
Indexable
-- Product lines that appear together in the same order
WITH order_lines AS (
  SELECT DISTINCT o.ordernumber, p.productline
  FROM orderdetails od
  JOIN orders o ON od.ordernumber = o.ordernumber
  JOIN products p ON od.productcode = p.productcode
)
SELECT
  a.productline AS lineA,
  b.productline AS lineB,
  COUNT(DISTINCT a.ordernumber) AS pair_orders
FROM order_lines a
JOIN order_lines b
  ON a.ordernumber = b.ordernumber
 AND a.productline < b.productline
GROUP BY a.productline, b.productline
HAVING COUNT(DISTINCT a.ordernumber) >= 15
ORDER BY pair_orders DESC;
Editor is loading...
Leave a Comment