I have 2 query
First one is:
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
Second one is:
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
Each of them will produce
(first query) (second query)
UserId | Total UserId | Total
-------------- --------------
1 | 10 1 | 2
2 | 15 2 | 5
My question is how to join them to table like this?
Userid | Total | Joined
-----------------------
1 | 10 | 2
2 | 15 | 5
Your query is extremely complicated without reason.
Try this:
Here's an SQLFiddle demonstrating this technique.
Whenever you find yourself with nested subqueries, ask yourself if that's really mandatory.