Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Contar valores que existem antes e depois de um determinado período de tempo


Basta adicionar isso ao 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 ;