0

I am trying to group records by ID and want to further group them by endDate is greater than or less than current time in mysql. How can I achieve that ? For example :

Rows     ID        endDate
Row 1    AA        2020-01-02
Row 2    AA        2020-03-01

I want the result to be :

AA , 1 (past date), 1 (future date)
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709

1 Answers1

0

I think you want conditional aggregation:

select id,
       sum(enddate < now()) as before_cnt,
       sum(enddate > now()) as after_cnt
from t
group by id;
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709