Count values which exist before and after a certain time period

前端 未结 1 861
轻奢々
轻奢々 2021-01-25 07:44

I have the following simple table which you can also find in the SQL Fiddle here:

CREATE TABLE Orders (
    Customer TEXT,
    Order_Date DATE
);

I         


        
相关标签:
1条回答
  • 2021-01-25 08:12

    Just add that to the select:

    SELECT o.Customer,
           SUM( o.Order_Date >= '2019-02-01' AND o.Order_Date < '2019-03-01' ) as num_feb_orders
    FROM ORDERS o
    GROUP BY o.Customer
    HAVING SUM( o.Order_Date >= '2019-02-01' AND o.Order_Date < '2019-03-01' ) > 0 AND
           SUM( o.Order_Date >= '2018-01-02' AND o.Order_Date < '2019-02-01' ) = 0 AND
           SUM( o.Order_Date < '2018-01-02' ) > 0 ;
    
    0 讨论(0)
提交回复
热议问题