0

我正在尝试构建一个 SQL Sum 语句来汇总来自不同表的数据。每个表都有一个共同的 SalesRep 字段。我在弄清楚如何正确构建 SQL 时遇到了一些麻烦。

我知道下面的语法是错误的并且不起作用,但我不是 SQL 专家。我确定这很简单。有人能帮助我吗?

SELECT SalesRepID, SUM(SELECT SalesRepID, Sum(tblClientAdditions.Amount) AS Subtotal
FROM tblClientAdditions GROUP BY SalesRepID + SELECT SalesRepID, 
Sum(tblAccounts.ReceivedAmount) AS Subtotal1
FROM tblAccounts GROUP BY SalesRepID) FROM tblSalesReps;
4

3 回答 3

1

表之间没有关系,对吧?

比可能是这样的吗?

SELECT (SELECT SUM(value) FROM table1) + (SELECT SUM(value) FROM table2) as result

问候

于 2012-12-10T13:31:23.703 回答
0
SELECT SalesRepID, 

(SELECT Sum(tblClientAdditions.Amount) FROM tblClientAdditions 
where tblClientAdditions.SalesRepID =tblSalesReps.SalesRepID)
+
(SELECT Sum(tblAccounts.ReceivedAmount) FROM tblAccounts
where tblAccounts.SalesRepID =tblSalesReps.SalesRepID)

FROM tblSalesReps;
于 2012-12-10T13:36:56.337 回答
0

我添加了 IsNull ,因为如果其中一列为空,您将在答案中得到空值,而您不希望这样。

SELECT ISNull(SUM(Table1.column1),0)+ (SELECT ISNull(SUM(Table2.column1),0) FROM Table2) FROM Table1 

这对我有用。

于 2013-10-17T11:22:35.660 回答