我有2個查詢
第一個是:
加入2計數查詢與GROUP BY子句
SELECT UserId, COUNT(CustomerId) AS Total
FROM (SELECT *
FROM Customer
WHERE JoinYear = 2016
AND JoinMonth = 1
AND JoinWeek = 2
AND JoinDay = 1) x
GROUP BY UserId
第二個是:
SELECT UserId, COUNT(CustomerId) AS Joined
FROM (SELECT *
FROM Customer
WHERE JoinYear = 2016
AND JoinMonth = 1
AND JoinWeek = 2
AND JoinDay = 1
AND JoinStatus = 2) x
GROUP BY UserId
他們每個人都將產生
(first query) (second query)
UserId | Total UserId | Total
-------------- --------------
1 | 10 1 | 2
2 | 15 2 | 5
我的問題是如何加入他們這樣的桌子?
Userid | Total | Joined
-----------------------
1 | 10 | 2
2 | 15 | 5
你可以試試這個 '選擇x1.UserId,COUNT(x1.CustomerId)爲總,COUNT(x2.CustomerId)AS加入 FROM(SELECT * FROM客戶WHERE JoinYear = 2016和JoinMonth = 1 AND JoinWeek = 2 AND AND JoinDay = 1)x1 LEFT JOIN(SELECT * FROM Customer WHERE JoinYear = 2016 AND JoinMonth = 1 AND JoinWeek = 2 AND JoinDay = 1 AND JoinStatus = 2)x2 ON x2.UserId = x1.UserId' –