如何简化此查询?

时间:2017-04-07 09:40:43

标签: postgresql

这是我的查询,

SELECT item_id,prod_name,closein,closeout,closein-closeout as closing_stock,stockin,closein-closeout+ stockin as current_balnce,stockout,
closein-closeout+ stockin-stockout  as balance,QtyIn,QtyOut 
    FROM (
        SELECT b.prod_name,a.item_id, 
        (select COALESCE ((select sum(qty) from stock_leger_head k
        join mas_prodt_name_hd b  ON  k.item_id = b.id 
        where k.dept_id=21 and k.flag= 'I' and cast(k.date as date)::date between '2017-03-01' and '2017-04-06' and  k.item_id=a.item_id),0)as QtyIn), 
        (select COALESCE ((select sum(qty) from stock_leger_head k
        join mas_prodt_name_hd b  ON  k.item_id = b.id 
        where k.dept_id=21 and k.flag= 'O' and cast(k.date as date)::date between '2017-03-01' and '2017-04-06' and  k.item_id=a.item_id),0)as QtyOut),
        (select COALESCE ((select sum(qty) from stock_leger_head k
        join mas_prodt_name_hd b  ON  k.item_id = b.id 
        where k.dept_id=21 and k.flag= 'I' and cast(k.date as date)::date <='2017-02-28' and  k.item_id=a.item_id),0)as closein),
        (select COALESCE ((select sum(qty) from stock_leger_head k
        join mas_prodt_name_hd b  ON  k.item_id = b.id 
        where k.dept_id=21 and k.flag= 'O' and cast(k.date as date)::date <='2017-02-28' and  k.item_id=a.item_id),0)as closeout),
                (select COALESCE ((select sum(qty) from stock_leger_head k
        join mas_prodt_name_hd b  ON  k.item_id = b.id 
        where k.dept_id=21 and k.flag= 'I' and cast(k.date as date)::date between '2017-03-01' and '2017-04-06'  and  k.item_id=a.item_id),0)as stockin),
        (select COALESCE ((select sum(qty) from stock_leger_head k
        join mas_prodt_name_hd b  ON  k.item_id = b.id 
        where k.dept_id=21 and k.flag= 'O' and cast(k.date as date)::date between '2017-03-01' and '2017-04-06'  and  k.item_id=a.item_id),0)as stockout)
        FROM stock_leger_head a INNER JOIN
        mas_prodt_name_hd b  ON  a.item_id = b.id 
        where a.dept_id=21
        GROUP BY a.item_id,b.prod_name order by item_id asc

谢谢

1 个答案:

答案 0 :(得分:1)

等待你的样本数据并创建表脚本,我想你可以从这样的事情开始:

SELECT C.PROD_NAME, B.*
    , B.closein - B.closeout AS closing_stock
    , B.closein - B.closeout + B.stockin AS current_balnce
    , B.closein - B.closeout + B.stockin - B.stockout AS balance
FROM 
   ( SELECT item_id
    ,SUM( CASE WHEN flag='I' AND cast(a.DATE AS DATE)::DATE <= '2017-02-28' THEN qty ELSE 0 END) AS closein
    ,SUM( CASE WHEN flag='O' AND cast(a.DATE AS DATE)::DATE <= '2017-02-28' THEN qty ELSE 0 END) AS closeout    
    ,SUM( CASE WHEN flag='I' AND cast(a.DATE AS DATE)::DATE BETWEEN '2017-03-01' AND '2017-04-06' THEN qty ELSE 0 END) AS stockin
    ,SUM( CASE WHEN flag='O' AND cast(a.DATE AS DATE)::DATE BETWEEN '2017-03-01' AND '2017-04-06' THEN qty ELSE 0 END) AS stockout
    ,SUM( CASE WHEN flag='I' AND cast(a.DATE AS DATE)::DATE BETWEEN '2017-03-01' AND '2017-04-06' THEN qty ELSE 0 END) AS QtyIn
    ,SUM( CASE WHEN flag='O' AND cast(a.DATE AS DATE)::DATE BETWEEN '2017-03-01' AND '2017-04-06' THEN qty ELSE 0 END) AS QtyOut
    FROM stock_leger_head a
    WHERE a.dept_id = 21
    GROUP BY a.item_id
    ) B
LEFT JOIN  mas_prodt_name_hd C ON B.item_id = C.id   
ORDER BY item_id 
;