Could some one please assist me creating a mysql monthly report for active users. A user visits a doctor on a particular date. that date is recorded on the visit date. they are then immediately issued an appointment date of when they should come back. A users active months are those from the month of visit all the way up to appointment date month , plus 90 days grace period.. if they dont pitch on the appointment date, they are given a grace period of 90 days, in which they'll still be considered as active users. after that they wont be considered as active anymore.
Users Table
+------------+------------+------------+
| UserID | visit |Appointment |
+------------+------------+------------+
| 10001 | 01-01-2010 | 01-02-2010 |
| 10001 | 05-02-2010 | 01-03-2010 |
| 10002 | 20-07-2010 | 15-10-2010 |
| 10003 | 01-11-2010 | 10-11-2010|
+------------+------------+------------+
The desired results would be
Monthly Report
+------------+------------+------------+
| Month | active | |
+------------+------------+------------+
| 2010-01 | 1 | |
| 2010-02 | 1 | |
| 2010-03 | 1 | |
| 2010-04 | 1 | |
| 2010-05 | 1 | |
| 2010-07 | 1 | |
| 2010-08 | 1 | |
| 2010-09 | 1 | |
| 2010-10 | 1 | |
| 2010-11 | 2 | |
| 2010-12 | 2 | |
| 2011-01 | 1 | |
+------------+------------+------------+
Here is the sql code
CREATE TABLE `visits` (
`id` int(10) NOT NULL,
`userid` int(10) NOT NULL,
`visit` date NOT NULL,
`appointment` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `visits` (`id`, `userid`, `visit`, `appointment`) VALUES
(1, 10001, '2010-01-01', '2010-02-01'),
(2, 10001, '2010-02-05', '2010-03-01'),
(3, 10002, '2010-07-20', '2010-10-15'),
(4, 10003, '2010-11-01', '2010-11-10');
ALTER TABLE `visits`
ADD PRIMARY KEY (`id`);
ALTER TABLE `visits`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10007;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;