如何在使用连接时SUM数据库列? [英] How to SUM database column when join is used?
本文介绍了如何在使用连接时SUM数据库列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在使用连接时SUM数据列?
How to SUM data column when join is used?
SELECT SUM(R.r_LimeGypsum),C.a_pH,C.c_FirstName,R.r_LimeGypsum
FROM dbo.Cultivator_tbl C
LEFT JOIN dbo.Recommendation_tbl R
ON C.Cid=R.Did
我收到此错误:
I am getting this error:
ERROR:Column 'dbo.Cultivator_tbl.a_pH' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
推荐答案
如果要显示旁边的字段聚合函数( SUM [ ^ ])你需要使用 GROUP BY [ ^ ]子句。
这里是对GROUP BY子句的解释: http://www.w3schools.com/sql/sql_groupby.asp [ ^ ]。
你的陈述将成为:
If you want to display fields along side the aggregate function (SUM[^]) you need to use the GROUP BY[^] clause.
Here is an explanation of the GROUP BY clause: http://www.w3schools.com/sql/sql_groupby.asp[^].
Your statement would become:
SELECT SUM(R.r_LimeGypsum),C.a_pH,C.c_FirstName,R.r_LimeGypsum
FROM dbo.Cultivator_tbl C
LEFT JOIN dbo.Recommendation_tbl R
ON C.Cid=R.Did
GROUP BY C.a_pH,C.c_FirstName,R.r_LimeGypsum
按照错误消息中的说明添加Group By子句,我假设你的陈述是正确的。
Add the Group By clause as explained in the error message, I am assuming that your statement is correct.
SELECT SUM(R.r_LimeGypsum),C.a_pH,C.c_FirstName,R.r_LimeGypsum
FROM dbo.Cultivator_tbl C
LEFT JOIN dbo.Recommendation_tbl R
ON C.Cid=R.Did
GROUP BY C.a_pH,C.c_FirstName,R.r_LimeGypsum
按照上面的解决方案1,添加我的解决方案时没有解决方案,他打败了我,并给了你有用的链接。[/编辑]
Follow solution 1 above, there was no solution when adding my solution, he beat me to it, and gave you useful links.[/Edit]
这篇关于如何在使用连接时SUM数据库列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文