postgresql - How To simplify in this query? -


this query,

select item_id,prod_name,closein,closeout,closein-closeout closing_stock,stockin,closein-closeout+ stockin current_balnce,stockout, closein-closeout+ stockin-stockout  balance,qtyin,qtyout      (         select b.prod_name,a.item_id,          (select coalesce ((select sum(qty) stock_leger_head k         join mas_prodt_name_hd b  on  k.item_id = b.id          k.dept_id=21 , k.flag= 'i' , cast(k.date date)::date between '2017-03-01' , '2017-04-06' ,  k.item_id=a.item_id),0)as qtyin),          (select coalesce ((select sum(qty) stock_leger_head k         join mas_prodt_name_hd b  on  k.item_id = b.id          k.dept_id=21 , k.flag= 'o' , cast(k.date date)::date between '2017-03-01' , '2017-04-06' ,  k.item_id=a.item_id),0)as qtyout),         (select coalesce ((select sum(qty) stock_leger_head k         join mas_prodt_name_hd b  on  k.item_id = b.id          k.dept_id=21 , k.flag= 'i' , cast(k.date date)::date <='2017-02-28' ,  k.item_id=a.item_id),0)as closein),         (select coalesce ((select sum(qty) stock_leger_head k         join mas_prodt_name_hd b  on  k.item_id = b.id          k.dept_id=21 , k.flag= 'o' , cast(k.date date)::date <='2017-02-28' ,  k.item_id=a.item_id),0)as closeout),                 (select coalesce ((select sum(qty) stock_leger_head k         join mas_prodt_name_hd b  on  k.item_id = b.id          k.dept_id=21 , k.flag= 'i' , cast(k.date date)::date between '2017-03-01' , '2017-04-06'  ,  k.item_id=a.item_id),0)as stockin),         (select coalesce ((select sum(qty) stock_leger_head k         join mas_prodt_name_hd b  on  k.item_id = b.id          k.dept_id=21 , k.flag= 'o' , cast(k.date date)::date between '2017-03-01' , '2017-04-06'  ,  k.item_id=a.item_id),0)as stockout)         stock_leger_head inner join         mas_prodt_name_hd b  on  a.item_id = b.id          a.dept_id=21         group a.item_id,b.prod_name order item_id asc 

thanks

waiting sample data , create table scripts, think start this:

select c.prod_name, b.*     , b.closein - b.closeout closing_stock     , b.closein - b.closeout + b.stockin current_balnce     , b.closein - b.closeout + b.stockin - b.stockout balance     ( select item_id     ,sum( case when flag='i' , cast(a.date date)::date <= '2017-02-28' qty else 0 end) closein     ,sum( case when flag='o' , cast(a.date date)::date <= '2017-02-28' qty else 0 end) closeout         ,sum( case when flag='i' , cast(a.date date)::date between '2017-03-01' , '2017-04-06' qty else 0 end) stockin     ,sum( case when flag='o' , cast(a.date date)::date between '2017-03-01' , '2017-04-06' qty else 0 end) stockout     ,sum( case when flag='i' , cast(a.date date)::date between '2017-03-01' , '2017-04-06' qty else 0 end) qtyin     ,sum( case when flag='o' , cast(a.date date)::date between '2017-03-01' , '2017-04-06' qty else 0 end) qtyout     stock_leger_head     a.dept_id = 21     group a.item_id     ) b left join  mas_prodt_name_hd c on b.item_id = c.id    order item_id  ; 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -