我無法選擇已訂購最多常見產品的幾位客戶。已訂購最多常見產品的夫婦 - SQL
實施例:
- customer1表訂購的產品:A,B,C,Z
- 顧客2訂購產品:A,C,d,克
- customer1表訂購的產品:G,H,Z
預期結果:Customer1 | Customer2 | 2(普通產品的數量排序)
我試了一下:
SELECT c.pid, c.cid, d.cid, count(c.pid)
FROM orders c JOIN orders d join
(SELECT a.cid, b.cid FROM customers a JOIN customers b ON b.cname != a.cname AND b.cname > a.cname) as subq
ON c.cid = a.cid and d.cid = b.cid
AND c.pid = d.pid group by c.pid;
這將返回每對夫妻的名字:
select a.cname client_1, b.cname client_2
from customers a join customers b on b.cname != a.cname and b.cname > a.cname;
我怎樣才能得到共同的PID的最大的一對夫婦的cids?
表:
customers (cid, cname, residence)
orders (pid, cid, odate, quantity)
products (pid, pname, price, origin)
請使用帶格式的文本而不是圖片。 (或者,更糟的是,鏈接到圖像。) – jarlh
對不起,我改變了它 – Lou