dyesin
1/13/2014 - 6:42 AM

Script to select all the new patients for the last 4 years. Note that only past patients are included (AptDateTime < curdate())

Script to select all the new patients for the last 4 years. Note that only past patients are included (AptDateTime < curdate())

SELECT
    YEAR (apt.AptDateTime) as year,
    MONTH(apt.AptDateTime) as month,
    count(*) as count
FROM 
    appointment apt, 
    patient pts
WHERE 
    apt.PatNum = pts.patNum and
    apt.IsNewPatient = 1 AND
    HOUR(apt.AptDateTime) BETWEEN 8 AND 18 and
    YEAR(apt.AptDateTime)  >= YEAR(DATE_SUB(CURDATE(), INTERVAL 4 YEAR)) AND
    apt.AptDateTime < curdate()
GROUP BY 
    month, year
ORDER by 
    month, year;