我有這個疑問SUM兩個表
SELECT Month(date) AS Monthly, Year(date) AS Annual, COUNT(idcad) AS NumCad, SUM(CONVERT(FLOAT, valorpag)) AS Valor FROM PI_AS
WHERE (status = 'Paid' OR status = 'Available') AND platform = 'Sales'
GROUP BY Year(date), Month(date)
ORDER BY Year(date), Month(date)
樣品結果:
Monthly | Annual | NumCad | Valor
3 | 2014 | 62 | 72534
4 | 2014 | 7 | 8253.6
5 | 2014 | 42 | 45356.39
6 | 2014 | 36 | 33343.19
7 | 2014 | 5 | 4414.6
與此查詢
SELECT Month(date) AS Monthly, Year(date) AS Annual, COUNT(idcad) AS NumCad, SUM(CONVERT(FLOAT, valorpag)) AS Valor FROM PI_PP
WHERE (status = 'Completed') AND platform = 'Sales'
GROUP BY Year(date), Month(date)
ORDER BY Year(date), Month(date)
樣品結果:
Monthly | Annual | NumCad | Valor
4 | 2014 | 6 | 2572.80
5 | 2014 | 8 | 7828
6 | 2014 | 3 | 3891.60
7 | 2014 | 2 | 278.3
我試圖UNION查詢:
SELECT Month(date) AS Monthly, Year(date) AS Annual, COUNT(idcad) AS NumCad, SUM(CONVERT(FLOAT, valorpag)) AS Valor FROM PI_AS
WHERE (status = 'Paid' OR status = 'Available') AND platform = 'Sales'
GROUP BY Year(date), Month(date)
UNION
SELECT Month(date) AS Monthly, Year(date) AS Annual, COUNT(idcad) AS NumCad, SUM(CONVERT(FLOAT, valorpag)) AS Valor FROM PI_PP
WHERE (status = 'Completed') AND platform = 'Sales'
GROUP BY Year(date), Month(date)
ORDER BY Year(date), Month(date)
但是當我這樣做,它與同一個月重複的行...我要的NumCad
和Valor
同月
的聯盟SUM導致這樣的事情:
Monthly | Annual | NumCad | Valor
6 | 2014 | 3 | 3891.60
6 | 2014 | 36 | 33343.19
7 | 2014 | 5 | 4414.6
7 | 2014 | 2 | 278.3
,但我想這一點:
Monthly | Annual | NumCad | Valor
6 | 2014 | 39 | 37234.79
7 | 2014 | 7 | 4692.9
有什麼想法?
工作完美,謝謝! – Adolfo