題目mysql
在表 orders 中找到訂單數最多客戶對應的 customer_number 。sql
數據保證訂單數最多的顧客剛好只有一位。code
SELECT customer_number FROM orders GROUP BY customer_number ORDER BY COUNT(customer_number) DESC LIMIT 1 OFFSET 0; --取1行數據,從第0行開始
思路blog
分步驟:class
(SELECT DISTINCT a.* FROM Insurance a, Insurance b WHERE a.TIV_2015 = b.TIV_2015 AND a.PID != b.PID) A
(SELECT DISTINCT b.PID FROM Insurance a, Insurance b WHERE a.LAT = b.LAT AND a.LON = b.LON AND a.PID != b.PID) B
最後代碼im
SELECT SUM(A.TIV_2016) AS TIV_2016 FROM (SELECT DISTINCT a.* FROM Insurance a, Insurance b WHERE a.TIV_2015 = b.TIV_2015 AND a.PID != b.PID) A LEFT JOIN (SELECT DISTINCT b.PID FROM Insurance a, Insurance b WHERE a.LAT = b.LAT AND a.LON = b.LON AND a.PID != b.PID) B ON (A.PID = B.PID) WHERE B.PID IS NULL;