简体   繁体   中英

Mysql how to find cumulative total by group

Can anyone help me to sort this out pleaase. i have a episode table and for an episode there will be following appointments . Episode table will be like

+-------------+------------+------------+------------+----------------+------+
| Episode_id  |  Patientid |  St_date   |  End_date  |      Status    | ...  |
+-------------+------------+------------+------------+----------------+------+
| 61112345    | 100001     | 12-01-2010 |            | Active         |      |
| 61112346    | xxxxxx     | 20-01-2010 | 10-10-2011 | Withdrawn      |      |
| .........   | xxxxxxxx   | 30-01-2010 | 10-05-2011 | Lost to follow |      |
| .........   | xxxxxxxx   | 01-02-2011 | Active     | Active         |      |
+-------------+------------+------------+------------+----------------+------+

Status field holds the status of each episode.A episode has 6 appointments , 3 months per appointment. so totally an episode has 18 months . some patient may complete all 6 appointment , some may withdraw in the middle, or some will be lost to follow up. i need to create a dashboard .

Appointment table will have fields for

Appointment_id
PatientId
...
Stats  // Completed or pending, which is used for reporting 

For example if a patient complete 2 appointment and if he is marked as Withdrawn on episdode which means that he has withdrawn from 3rd visit and active for 2 visits, if we lost to follow him on 5th app, then he will be active for 4app and then he will be added to lost to follow up on 5th visit. if he completes all then he will added to active for all 6 visits. and the report should be like

Report from 01-01-2010 to 31-12-2010
+--------+--------+-------------+----------------+---------+
|        | Active |  Withdrawn  | Lost to follow | Revised |
+------- +--------+-------------+----------------+---------+
| visit1 | 1500   | 30          | 5              | 5       |
| Visit2 | 1800   | 20          | 4              | 3       |
| Visit3 | 1900   | 45          | 3              | 2       |
| Visit4 | 1800   | 34          | 0              | 1       |
| Visit5 | 1900   | 30          | 0              | 1       |
| Visit6 | 1200   | 20          | 0              | 5       |
+--------+--------+-------------+----------------+---------+

Currently we are fetching the query and using loop only we are generating reports like this, but it is taking time to process, is there any way i can achieve using query itself.

It isn't really clear what you want to group by, but I can give you a general answer. After your where clause you can add "group by fieldname order by fieldname" where fieldname is the element you want to count or sum. You can then count(fieldname) or sum(fieldname) to either add or count.

This may be helpful: http://www.artfulsoftware.com/infotree/qrytip.php?id=105

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM