Mysql Query for Aging To Count No of records


Here is query and output


SELECT
         SUM(CASE WHEN TO_DAYS(CURDATE()) - TO_DAYS(DOS) BETWEEN 1 AND 30 THEN 1 ELSE 0 END) AS age130,
         SUM(CASE WHEN TO_DAYS(CURDATE()) - TO_DAYS(DOS) BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS age3160,
         SUM(CASE WHEN TO_DAYS(CURDATE()) - TO_DAYS(DOS) BETWEEN 61 AND 90 THEN 1 ELSE 0 END) AS age6190,
         SUM(CASE WHEN TO_DAYS(CURDATE()) - TO_DAYS(DOS) BETWEEN 91 AND 120 THEN 1 ELSE 0 END) AS age91120,
         SUM(CASE WHEN TO_DAYS(CURDATE()) - TO_DAYS(DOS) > 120 THEN 1 ELSE 0 END) AS agegt120
  from encounter where claimNo is null