Sum for multiple date ranges in a single call?

前端 未结 10 1768
有刺的猬
有刺的猬 2021-02-14 10:36

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:28

    Well coming from a SQL Server background I would change your where clause to

    ...
    AND (
          "balance_transactions"."created" BETWEEN '2013-12-20' AND '2014-01-19'
          OR
          "balance_transactions"."created" BETWEEN '2013-12-21' AND '2014-01-20'
          OR
          "balance_transactions"."created" BETWEEN '2013-12-23' AND '2014-01-22'
          OR
          "balance_transactions"."created" BETWEEN '2013-12-24' AND '2014-01-23'
        );
    

    Just be sure you have a good index on those dates! :)

提交回复
热议问题