简体   繁体   中英

How to write a query to display SUM of the quantity if same product having same price in my inventory?

My table looks like

Product       Price        Qty
A              100          10
B              200          30
A              100          15
A              150          20

Two times Price 100 is repeating for product A in my table. It should add the quantity if price value same for the product. My result should be like as follows

Product       Price         Qty
A              100           25
A              150           20
B              200           30

Just use group by product, price with sum aggregation as:

select product, price, sum(qty)
  from tab
group by product, price
order by product, price;

use sum() function with group by product and price

    select t.product, t.price,sum(t.qty) as Qty from your_table t
    group by t.product,t.price

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM