Sum for multiple date ranges in a single call?

前端 未结 10 1345
礼貌的吻别
礼貌的吻别 2021-02-14 11:03

I have the following query:

SELECT 
   SUM(\"balance_transactions\".\"fee\") AS sum_id 
   FROM \"balance_transactions\" 
   JOIN charges ON balance_transactions         


        
10条回答
  •  清歌不尽
    2021-02-14 11:35

    Here's a untested procedure you can used.

    CREATE OR REPLACE PROCEDURE sum_fees(v_start IN Date, v_end in Date) IS
    
    BEGIN
      SELECT 
       SUM("balance_transactions"."fee") AS sum_id 
       FROM "balance_transactions" 
           JOIN charges ON balance_transactions.source = charges.balance_id 
       WHERE "balance_transactions"."account_id" = 6 
          AND (balance_transactions.type = 'charge' 
          AND charges.refunded = false 
          AND charges.invoice IS NOT NULL) 
          AND ("balance_transactions"."created" BETWEEN v_start AND v_end);
    END;
    

    Then call the procedure with your range date.

提交回复
热议问题