多列之间的总和(查询SQL) [英] Sum between multi columns (query SQL)
问题描述
你好,我有id - 名字 - 国家 - 购买价值 - 运费价值 - 其他费用 - 总额
22 1111
5 20
10
你可以看到我想在这些列之间求和并将其显示为总列但是列可能为空不应该全部3列我怎么能这样做< b
谢谢你。 ...
我尝试过:
hello,i have id - name - country - purchase value - shipping value - other charge -Total
22 1111
5 20
10
as u can see i want to sum between those columns and display it into total column but maybe column will be null empty not should have all 3 column full how can i do this <b
thank u....
What I have tried:
SELECT Id,Name,Country,purchesvalue+shippingvalue+othercharge as Total
From test;
SELECT Id,Name,Country,purchesvalue+shippingvalue as Total
FROM test
这只是覆盖了查询所以有什么方法可以让它变得简单
我试试这个告诉我语法错误
this just overwrite the query so is there any method to make it simple
and i try this this told me syntax error
SELECT (Id,Name,Country,ISNULL(purchesvalue,0) + ISNULL(shippingvalue,0) + ISNULL(shippingvalue,0)) as 'Total'
FROM test
推荐答案
尝试:
Try:
SELECT Id, Name, Country, ISNULL(purchesvalue, 0) + ISNULL(shippingvalue, 0) + ISNULL(shippingvalue, 0)) as [Total]
您的后续查询似乎是正确的。试试这个
Your later query seems correct. Try this
SELECT Id,Name,Country,ISNULL(purchesvalue,0) + ISNULL(shippingvalue,0) + ISNULL(shippingvalue,0) as 'Total'
FROM test
更好的方法是使VIEW隐藏所有上述总和的复杂性并使查询更具可读性。
A better approach is to have a VIEW that hide all above sums complexity and make query more readable.
SELECT Id,Name,IsNULL(p_value,0)+ IsNULL(s_vale,0)+ IsNULL(o_value,0)为'Total'
FROM产品
试试这个对我有用。
SELECT Id,Name,IsNULL(p_value,0) + IsNULL(s_vale,0) + IsNULL(o_value,0) as 'Total'
FROM product
Try this works for me.
这篇关于多列之间的总和(查询SQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!