如何使用嵌套查询避免PostgreSQL中的DRY

时间:2016-03-01 21:09:21

标签: sql postgresql

此查询以这种方式正常工作:

SELECT DISTINCT inv.move_id,
                inv.client_id, 
                inv.reference,
                inv.date_inv,  
                inv.amount,
                inv.state,
                inv.client_type,
                inv.agent_id,
                (
                      SELECT SUM(nc.amount) AS total
                      FROM invoice nc
                      WHERE nc.journal = 15
                      AND nc.ref_move = inv.move_id
                      AND nc.state = inv.state
                ) AS credit, ( 
                      SELECT SUM(ip.amount)
                      FROM inv_payment ip
                      WHERE inv.reference = ip.reference
                ) AS paid,
                (inv.amount) - (
                  coalesce((
                    SELECT SUM(nc.amount) AS total
                    FROM invoice nc
                    WHERE nc.journal = 15
                    AND nc.ref_move = inv.move_id
                    AND nc.state = inv.state
                  ), 0) + coalesce((
                    SELECT SUM(ip.amount)
                    FROM inv_payment ip
                    WHERE inv.reference = ip.reference
                  ), 0)
                ) AS outstanding
FROM invoice inv
  INNER JOIN inv_move im ON im.id = inv.move_id
  INNER JOIN client cl ON cl.id = inv.client_id 
WHERE inv.agent_id = '%'
  AND inv.date_inv
    BETWEEN '%' AND '%'
  AND inv.state = 'open'
ORDER BY inv.agent_id

我试图干这样做:

SELECT DISTINCT inv.move_id,
                    inv.client_id, 
                    inv.reference,
                    inv.date_inv,  
                    inv.amount,
                    inv.state,
                    inv.client_type,
                    inv.agent_id,
                    (
                      SELECT SUM(nc.amount) AS total
                      FROM invoice nc
                      WHERE nc.journal = 15
                      AND nc.ref_move = inv.move_id
                      AND nc.state = inv.state
                    ) AS credit, ( 
                      SELECT SUM(ip.amount)
                      FROM inv_payment ip
                      WHERE inv.reference = ip.reference
                    ) AS paid,
                    (inv.amount) - (
                      coalesce((credit), 0) + coalesce((paid), 0)
                    ) AS outstanding
    FROM invoice inv
      INNER JOIN inv_move im ON im.id = inv.move_id
      INNER JOIN client cl ON cl.id = inv.client_id 
    WHERE inv.agent_id = '%'
      AND inv.date_inv
        BETWEEN '%' AND '%'
      AND inv.state = 'open'
    ORDER BY inv.agent_id

但我以这种方式收到错误。 因为我想在其他列中使用别名列作为操作值。 我怎么能这样做? 我试图使用WITH PostgreSQL语句,但我无法以这种方式找到更好的解决方案。

2 个答案:

答案 0 :(得分:3)

CANT 使用在同一级别计算的字段。

SELECT 1 + 2  as three, three * 2 as six
                        ^^^^^
                        undefined

所以你要么创建一个子查询。

SELECT three*2 as six
FROM ( SELECT 1 + 2 as three
       FROM table
     ) T

或您的示例使用JOIN

使用CTE:

WITH cte as (
    SELECT ref_move, state, SUM(nc.amount) AS total
    FROM invoice nc
    WHERE nc.journal = 15
    GROUP BY ref_move, state 
)
SELECT *
FROM invoice i 
JOIN cte c
  ON i.ref_move= c.ref_move
 AND i.state  = c.state 

答案 1 :(得分:1)

在FROM中使用子查询:

        var winH = $(window).height();
        $('.page').height(winH);

        var c = 0;
        var pagesN = $('.page').length;
        var canScroll = true;

        $(document).bind('mousewheel', function(ev, delta) {
         ev.preventDefault();
         if(!canScroll) return;
         delta>0 ? --c : ++c ;    
         if(c===-1){
         c=0;
         }else if(c===pagesN){
          c=pagesN-1;
         }
         canScroll = false;
           var pagePos = $('.page').eq(c).position().top;       
          $('html, body').stop().animate({scrollTop: pagePos},700,     

         function(){
         canScroll = true;
         });
         return false; 

         });