[英]sql query to sum of same id but different value?
sql query to sum of same id but different value??.... I am trying to calculate sum of same id...following is my table sql查询到相同id但不同值的总和??....我正在尝试计算相同id的总和...以下是我的表
id barter_value
2 50,000
2 1,50,000
3 47,000
3 55,000
3 50,00,000
I want output like我想要输出像
id barter_value
2 2,00,000
3 51,00,2000
select a.buyer_id,
a.prod_barter_val
from add_to_cart as a
join(select buyer_id,
sum(prod_barter_val) as total
from add_to_cart group by buyer_id) as b
on a.buyer_id = b.buyer_id;
你可以这样做:
select buyer_id, sum(prod_barter_val) from add_to_cart group by buyer_id
You're almost there, just change the sql statment ( b.total
): b.total
,只需更改 sql 语句 ( b.total
):
select buyer_id,
sum(prod_barter_val) as total
from add_to_cart
group by buyer_id
SELECT id, SUM(barter_value) as 'total' FROM table GROUP BY id
What this does: Selects all the ID's and the barter values from the table.作用:从表中选择所有 ID 和易货值。 Then the GROUP BY takes effect, and all ID's that are equal are joined into one row.
然后 GROUP BY 生效,所有相等的 ID 合并为一行。
Without the SUM you would end up with just a random value in the barter_value column, but with SUM it adds all the rows together for the ID's (seperately) and puts that in the total column.如果没有 SUM,您最终只会在 barter_value 列中获得一个随机值,但使用 SUM 它将所有行加在一起(单独)作为 ID,并将其放入总列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.