计算表中所有行的 TotalAmount
你能帮我解决这个问题吗?我试图计算价格表中所有行的总和,但这些值永远不会相加,现在似乎也不再计算。
我有以下内容:
@{
decimal money = 0.00m;
var prices = "SELECT Price, SUM(Price) FROM PriceTable GROUP BY Price";
var database = Database.Open("MYDB");
database.QuerySingle(prices);
money = prices.AsDecimal();
}
在 HTML 的某处输入:
@money // to display the totalAmount
我这样做对吗?我已经搜索过这个,但令人惊讶的是似乎没有太多关于此的信息,但我可能没有使用正确的关键字。
谢谢
Can you please help me with this? I am trying to calculate the total of all the rows in my Price Table, but the values never add up, and now it doesn't seem to calculate anymore either.
I have the following:
@{
decimal money = 0.00m;
var prices = "SELECT Price, SUM(Price) FROM PriceTable GROUP BY Price";
var database = Database.Open("MYDB");
database.QuerySingle(prices);
money = prices.AsDecimal();
}
And somewhere in my HTML I type:
@money // to display the totalAmount
Am I doing it right? I've searched for this, but surprisingly there seems to be not much info about this, I'm probably not using the right keywords though.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改
为
IF 您想要的只是所有行的总和,则不需要分组。
还;
change
to
IF all you want is the sum of all rows, the grouping is not required.
Also;
那是因为您也添加了价格本身。您仅对不同的价格求和,因此对于每个唯一的价格,总和与价格相同。
简而言之:删除价格和分组依据。只需查询整个表的 sum(price) 即可。
That's because you add the price itself too. You sum only the distinct prices, so for every price that is unique, the sum is the same as the price.
To be short: Remove the price and the group by. Just query sum(price) over the whole table.
{@money}
<-- 这不是正确的语法吗?不是“@money”,只要我没记错的话......以及下面带有查询的评论! =]
{@money}
<-- isn't this the correct syntax? not "@money" only if I remember correctly...and also the comments below with the query! =]