我想合併兩個非常相似的查詢。唯一不同的就是WHERE
子句中:兩個不同的where子句的SQL Server查詢
SELECT
CONVERT(VARCHAR(10), Date, 101) as Date,
COUNT(*) as WinnerCount
FROM
table t1
WHERE
t1.Result = 'Winner'
GROUP BY
CONVERT(VARCHAR(10), Date, 101)
,第二個具有失敗者條款:
SELECT
CONVERT(VARCHAR(10), Date, 101) as Date,
COUNT(*) as LoserCount
FROM
table t1
WHERE
t1.Result = 'Loser'
GROUP BY
CONVERT(VARCHAR(10), Date, 101)
我試圖做到的,是得到一個結果集是這樣的:
Date | WinnerCount | LoserCount
01/01/2017 24 16
17/02/2017 13 9
我試圖合併這與加入兩個結果集:
SELECT
FirstSet.Date
FROM
(SELECT
CONVERT(VARCHAR(10), Date, 101) as Date,
COUNT(*) as Count
FROM
table t1
WHERE
t1.Result = 'Winner'
GROUP BY
CONVERT(VARCHAR(10), Date, 101)) AS FirstSet
JOIN
(SELECT
CONVERT(VARCHAR(10), Date, 101) as Date,
COUNT(*) as Count
FROM
table t1
WHERE
t1.Result = 'Loser'
GROUP BY
CONVERT(VARCHAR(10), Date, 101)) AS SecondSet ON FirstSet.Date = SecondSet.Date
ORDER BY
FirstSet.Date
但在結果中我只有日期:/