SQL SUM 问题 [英] SQL SUM question
本文介绍了SQL SUM 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于 sql 中的 SUM 的问题,
Hi I have a question about SUM in sql,
我有一个类似这样的查询
I have a query that looks like this
SELECT
SUM ( table_one.field + table_two.field ) as total_field
SUM ( total_field + table_one.anotherfield )
FROM
table_one
JOIN
table_two ON table_one.id = table_two.id
WHERE
table_one = 1
但这不起作用(不要介意 JOIN 语句中可能的输入错误,只有第二个 SUM 是查询在没有该 SUM 的情况下完美运行的可能性)
But this doesn't work ( dont mind possible typing errors in JOIN statement, only the second SUM is the probly the query works perfecly without that SUM)
还有其他方法可以做到这一点,因为我需要在我的应用程序中使用 total_field.我当然可以在应用程序中添加这些数字,但我更喜欢在 sql 中添加.
Is there another way to do it, as I need the total_field within my application. I can ofcource add those numbers within the application but I prefer to do it in sql.
推荐答案
不能在聚合中使用列别名来引用值,只需再次 SUM;
You cannot use the column alias in an aggregate to reference the value, just SUM again;
SELECT
SUM ( table_one.field + table_two.field ) as total_field, --your missing a , also
SUM ( table_one.field + table_two.field + table_one.anotherfield )
FROM
table_one
JOIN
table_two ON table_one.id = table_two.id
WHERE
table_one = 1
这篇关于SQL SUM 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文