By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE tb_trades(
ID VARCHAR(38) NOT NULL PRIMARY KEY
,Symbol VARCHAR(8)
,Price INTEGER
,Volume INTEGER
,Charges INTEGER
);
INSERT INTO tb_trades(ID,Symbol,Price,Volume,Charges) VALUES ('1','BP',300,+100,15);
INSERT INTO tb_trades(ID,Symbol,Price,Volume,Charges) VALUES ('2','BP',310,+100,15);
INSERT INTO tb_trades(ID,Symbol,Price,Volume,Charges) VALUES ('3','TOT',250,+50,20);
INSERT INTO tb_trades(ID,Symbol,Price,Volume,Charges) VALUES ('4','BP',340,-20,10);
select
symbol,
sum(case when volume > 0 then volume * price end)
/ sum(case when volume > 0 then volume end) as avg_price,
sum(volume) as volume,
sum(charges) as total_charges
from tb_trades
group by symbol
symbol | avg_price | volume | total_charges |
---|---|---|---|
BP | 305.0000 | 180 | 40 |
TOT | 250.0000 | 50 | 20 |