postgresql – Add the alias of the previous column to calculate the sum / total of the last column

I have a query that I need to optimize. It uses 3 subqueries 2 times each. Here is the query. select i.itemid, b.itemname, i.batchno,
(select the sum (qty) in Inventtrans
where itemid = i.itemid
and batchno = i.batchno
and inventlocationid = locationId & # 39;
and dateinvent < '2018-12-26 00:00:00') as openingBal , (select sum(qty) from inventtrans where itemid = i.itemid and batchno = i.batchno and inventlocationid = 'locationId' and dateinvent >= & # 39; 2018-01-01 00: 00: 00 & # 39;
and dateinvent <= '2019-12-31 00:00:00' and qty>0
) as inputQty,
(select the sum (qty) in Inventtrans
where itemid = i.itemid
and batchno = i.batchno
and inventlocationid = locationId & # 39;
and dateinvent> = 2018-01-01 00: 00: 00 & # 39;
and dateinvent <= 2019-12-31 00: 00: 00 & # 39;
and quantity <0
) as outputQty,
(
COALESCE ((select the sum (qty) in Inventtrans
where itemid = i.itemid
and batchno = i.batchno
and inventlocationid = locationId & # 39;
and dateinvent < '2018-12-26 00:00:00'),0) + COALESCE((select sum(qty) from inventtrans where itemid = i.itemid and batchno = i.batchno and inventlocationid = 'locationId' and dateinvent >= & # 39; 2018-01-01 00: 00: 00 & # 39;
and dateinvent <= '2019-12-31 00:00:00' and qty>0), 0)
+
COALESCE ((select the sum (qty) in Inventtrans
where itemid = i.itemid
and batchno = i.batchno
and inventlocationid = locationId & # 39;
and dateinvent> = 2018-01-01 00: 00: 00 & # 39;
and dateinvent <= 2019-12-31 00: 00: 00 & # 39;
and quantity <0), 0)
) as a ball,
location,
- split_part (i.inventsizeid, & # 39; / +[0-9]& # 39 ;, 2) as a size
i.inventsizeid as size,
i.configid as color
of inventtrans i, view_inventbatchtrans b
or
i.batchno = b.batchid
group by i.itemid, b.itemname, i.batchno, i.qty, i. "location", i.inventsizeid, i.configid;

check the query for openingBal, inputQty, outputQt.
I need to add these 3 columns to get the ball column. Here is the query that I have optimized.

select i.itemid, b.itemname, i.batchno, i.openingBal, i.inputQty, i.outputQty, i.bal, i.location, i.inventsizeid in size, i.configid in color
from (select i. *,
(select the sum (qty) in Inventtrans
where itemid = i.itemid
and batchno = i.batchno
and inventlocationid = locationId & # 39;
and dateinvent < '2018-12-26 00:00:00') as openingBal, (select sum(qty) from inventtrans where itemid = i.itemid and batchno = i.batchno and inventlocationid = 'locationId' and dateinvent >= & # 39; 2018-01-01 00: 00: 00 & # 39;
and dateinvent <= '2019-12-31 00:00:00' and qty>0
) as inputQty,
(select the sum (qty) in Inventtrans
where itemid = i.itemid
and batchno = i.batchno
and inventlocationid = locationId & # 39;
and dateinvent> = 2018-01-01 00: 00: 00 & # 39;
and dateinvent <= 2019-12-31 00: 00: 00 & # 39;
and quantity <0
) as outputQty, (COALESCE (i.openingBal, 0) + COALESCE (i.inputQty, 0) + COALESCE (i.outputQty, 0)) as a ball
from Inventtrans i
) i, view_inventbatchtrans b
or
i.batchno = b.batchid
group by i.itemid, b.itemname, i.batchno, i.qty, i. "location", i.inventsizeid, i.configid;

Where I run optimized the query throwing an error like
SQL error [42703]: ERROR: The i.openingbal column does not exist
Position: 937 which is at (COALESCE (i.openingBal, 0) + COALESCE (i.inputQty, 0) + COALESCE (i.outputQty, 0)) as a ball

Could you say what I'm doing wrong. Can we use the alias column for this purpose?