I am writing a sql query that will group a column and return its min, max and average. That's what I'm doing:
CREATE TABLE Nums (
patient_id VARCHAR(20),
val DOUBLE
);
INSERT INTO Nums
VALUES ("A", 100), ("A", 175), ("B", 200), ("B", 100), ("B",20), ("B",2000), ("B",4000);
select AVG(event_count), MIN(event_count), MAX(event_count)
from Nums a
join (select patient_id, count(*) as event_count from Nums group by patient_id) b
on a.patient_id = b.patient_id;
I get output for the following query as
AVG(event_count) MIN(event_count) MAX(event_count)
4.1429 2 5
I get the correct values for min and max, however I expected the average to be 3.5. I'm not sure what I'm doing wrong. Any pointers to what I am missing?
Thank.
source
share