简体   繁体   中英

MYSQL select average number of entries

I have a table that has a unique key each time a user creates a case:

id|doctor_id|created_dt
--|---------|-----------
 1|23       |datetimestamp
 2|23       |datetimestamp
 3|17       |datetimestamp

How can I select and return the average amount of entries a user has per month?

I have tried this:

SELECT avg (id)
FROM `cases`
WHERE created_dt BETWEEN DATE_SUB(CURDATE(),INTERVAL 90 DAY) AND CURDATE() 
and doctor_id = 17

But this returns a ridiculously large value that cannot be true.

To clarify: I am trying to get something like doctor id 17 has an average of 2 entries per month into this table.

I think you were thrown off by the idea of "averaging". You don't want the average id, or average user_id. You want the average number of entries into the table, so you would use COUNT() :

SELECT count(id)/3 AS AverageMonthlyCases
FROM `cases`
WHERE created_dt BETWEEN DATE_SUB(CURDATE(),INTERVAL 90 DAY) AND CURDATE() 
group by doctor_id 

Since you have a 90 day interval, you want to count the number of rows per 30 days, or the count/3.

SELECT AVG(cnt), user_id
FROM (
   SELECT COUNT(id) cnt, user_id
   FROM cases
   WHERE created_dt BETWEEN <yourDateInterval>
   GROUP BY user_id, year(created_dt), month(created_dt)
)

Since you need average number of entries, AVG function is not really applicable, because it is SUM()/COUNT() and obviously you do not need that (why would you need SUM of ids).

You need something like this

SELECT 
    doctor_id, 
    DATE(created_dt,'%m-%Y') AS month, 
    COUNT(id) AS visits
FROM `cases`
GROUP BY 
    `doctor_id`, 
    DATE(created_dt,'%m-%Y')
ORDER BY
    `doctor_id` ASC, 
    DATE(created_dt,'%m-%Y') ASC

To get visits per month per doctor. If you want to average it, you can then use something like

SELECT 
    doctor_id, 
    SUM(visits)/COUNT(month) AS `average` 
FROM (
    SELECT 
        doctor_id, 
        DATE(created_dt,'%m-%Y') AS month, 
        COUNT(id) AS visits
    FROM `cases`
    GROUP BY 
        `doctor_id`, 
        DATE(created_dt,'%m-%Y')
    ORDER BY
        `doctor_id` ASC, 
        DATE(created_dt,'%m-%Y') ASC
    ) t1
GROUP BY
    doctor_id

Obviously you can add your WHERE clauses, as this query is compatible for multiple years (ie it will not count January of 2013th and January of 2014th as one month).

Also, it takes into account if a doctor has "blank" months, where he did not have any patients, so it will not count those months (0 can destroy and average).

Use this, you'll group each doctor's total id, by month.

 Select monthname(created_dt), doctor_id, count(id) as total from cases group by 1,2 order by 1

Also you can use GROUP_CONCAT() as nested query in order to deploy a pivot like table, where each column is each doctor_id.

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