SUM一些SQL选择列与两个表的UNION

时间:2014-07-02 16:58:27

标签: sql sql-server

我有这个查询

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)

但是当我这样做时,它会在同一个月重复该行......我希望同一个月的NumCadValor的和解

UNION结果如下:

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

有什么想法吗?

3 个答案:

答案 0 :(得分:5)

首先,您需要在数据上运行union,然后进行聚合:

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)

答案 1 :(得分:1)

这应该可以解决你想要的问题:

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

答案 2 :(得分:0)

将查询转到子查询并从itp>中选择

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