如何使sql查询获得两列的总差异 [英] how to make sql query for getting total of difference of two columns
问题描述
来自`sale_has_enquiryitem`的SELECT SUM(`ServiceTaxAmount`)WHERE`ServiceTax`<>'NULL'
我有两张桌子
table1'sale'有栏'saleamount'
table2'购买'有栏目'购买额'
as
促销购买
saleamount purchaseamount
40 10
20 20
60 15
来自上表,< br $>
40-10 == 30
50-20 == 30
60-15 == 45
i想要计算上述差异的总和(30 + 30 + 45 = 105)
SELECT SUM(`ServiceTaxAmount`) from `sale_has_enquiryitem` WHERE `ServiceTax`<>'NULL'
I had two tables as
table1 'sale' has column 'saleamount'
table2 'purchase' has column 'purchaseamount'
as
sale purchase
saleamount purchaseamount
40 10
50 20
60 15
from above table,
40-10==30
50-20==30
60-15==45
i want to calculate total of the above difference(30+30+45=105)
推荐答案
参见我对你之前这个问题的例子的回答:怎么样l列两列的差异 [ ^ ]
See my answer to your previous example of this question: how to calculate difference of two columns[^]
Hi,
You have 2 different ways to achieve your goal.
1)
<pre lang="SQL">SELECT ((SELECT Sum(SaleAmount) FROM Sale) - (SELECT Sum(purchaseamount) FROM Purchase))
2)以上查询看起来不太好看。因此,您可以将变量中的查询分开并使用它们
2) Above query will not look nice to read. So you can separate both the queries in variables and use them
Declare @SalesAmount money = (SELECT Sum(SaleAmount) FROM Sale)
Declare @PurchaseAmount money = (SELECT Sum(purchaseamount) FROM Purchase)
SELECT @SalesAmount - @PurchaseAmount
如果您有任何疑虑或疑问,请告知我们你需要任何其他的帮助。
谢谢
Advay Pandya
Please let me know if yo have any concern or query or if you need any other help.
Thanks
Advay Pandya
这篇关于如何使sql查询获得两列的总差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!