How to sum entries of different rows in MS Access?

问题: I have a table of orders in MS Access, the leftmost column representing the order ID. But some orders have more than one type of product, I need to compute the total cost...

问题:

I have a table of orders in MS Access, the leftmost column representing the order ID.

But some orders have more than one type of product, I need to compute the total cost of the order. I know how to get the price of one product (within a line: [quantity]*[unit price]) but how do I sum the total product price of multiple rows?

Thanks!

enter image description here


回答1:

You would use sum(). For instance:

select orderid, sum(quantity * unit_price)
from t
group by orderid;

This gives the total for each order. You can change the query to get the total for each product or whatever you care about.


回答2:

Use the sum aggregate function and group by the Products_ID field, e.g.:

select t.orderid, t.products_id, sum(t.quantity*t.[unit price]) as price
from YourTable t
group by t.orderid, t.products_id

Change YourTable to your table name.

  • 发表于 2019-03-22 05:17
  • 阅读 ( 250 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除