计算SUM的SUM

时间:2016-04-22 17:53:59

标签: sql

我正在努力计算具有SUM的查询的SUM。这是我的疑问:

SELECT rs.resellerid
       ,r.company
       ,r.insidesales
       ,SUM(total_reseller_sales) as TotalResellerSalesYear
  FROM sales_report_resellers rs
    INNER JOIN resellers r
      ON rs.resellerid = r.resellerid
  WHERE (sid > '282' AND sid < '292')
    AND r.insidesales = 1
  GROUP BY rs.resellerid, r.company, r.insidesales 

查询返回5条记录,每条记录金额为1美元。我需要所有5条记录的SUM。

2 个答案:

答案 0 :(得分:1)

Group By列表

中删除Select和非聚合列
SELECT  SUM(total_reseller_sales) as Total
FROM sales_report_resellers rs 
INNER JOIN resellers r 
ON rs.resellerid = r.resellerid 
WHERE (sid > '282' AND sid < '292') 
AND r.insidesales = 1 

答案 1 :(得分:0)

如果没有看到示例数据集,我猜您需要从SELECTGROUP BY中删除转销广告。

SELECT
   [r].[company]
   ,[r].[insidesales]
   ,SUM([total_reseller_sales]) AS [TotalResellerSalesYear]
FROM
    [sales_report_resellers] [rs]
    INNER JOIN [resellers] [r] ON [rs].[resellerid] = [r].[resellerid]
WHERE
    (
     [sid] > '282'
     AND [sid] < '292'
    )
    AND [r].[insidesales] = 1
GROUP BY
   [r].[company]
   ,[r].[insidesales];