简体   繁体   中英

SUM some columns of SQL Selects with UNION of two tables

I have this query

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)

sample result:

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

and this query

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)

sample result:

Monthly | Annual | NumCad | Valor
      4 |   2014 |      6 | 2572.80
      5 |   2014 |      8 | 7828
      6 |   2014 |      3 | 3891.60
      7 |   2014 |      2 |  278.3

I tried UNION the queries:

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)

But when I do this, it repeat the row with the same month... I want the SUM of NumCad and Valor for the same month

The UNION result in something like this:

Monthly | Annual | NumCad | Valor
      6 |   2014 |      3 |  3891.60
      6 |   2014 |     36 | 33343.19
      7 |   2014 |      5 |  4414.6
      7 |   2014 |      2 |   278.3

but I want this:

Monthly | Annual | NumCad | Valor
      6 |   2014 |     39 | 37234.79
      7 |   2014 |      7 |  4692.9

Any idea?

First you need to run union on data and then do the aggregation:

SELECT 
    Month(data.date) AS Monthly, 
    Year(data.date) AS Annual,
    COUNT(data.idcad) AS NumCad, 
    SUM(CONVERT(FLOAT, data.valorpag)) AS Valor 
FROM 
    (SELECT date, idcad, valorpag FROM PI_AS
    WHERE (status = 'Paid' OR status = 'Available') AND platform = 'Sales'
    UNION ALL
    SELECT date, idcad, valorpag  FROM PI_PP
    WHERE (status = 'Completed') AND platform = 'Sales'
    ) data
GROUP BY Year(data.date), Month(data.date)
ORDER BY Year(data.date), Month(data.date)

This should fix what you want:

SELECT Monthly, Annual, SUM(NumCad), SUM(Valor)
FROM
 (
  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)
 ) s
GROUP BY Annual, Monthly

Turn you query into a subquery aqnd select from it:

select monthly, annual, sum(numcad),sum(valor)
from
(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)
)a
group by monthly, annual
 ORDER BY  monthly, annual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM