There is a hospital database that has the following schema for admissions
table:
patient_id INT
admission_date DATE
discharge_date DATE
diagnosis TEXT
room_id INT
doctor_id INT
insurance_id INT
here’s what I am trying to query:
- fetch the total amount of admissions for each day.
- fetch the amount of admissions changed from the previous date.
I tried the following query:
SELECT
admission_date,
count(admission_date) as adm_day,
count(admission_date) - count(admission_date - 1) AS admn_count_change // this count is not correct
FROM admissions
group by admission_date
Query Result:
Data is showing correctly for the first two columns, the last column is showing incorrect data i.e a value of 0 in all rows and it is completely going over my head. I don’t use SQL much, please guide me.
2
Answers
You can use a combination of
Code (Oracle syntax):
Personally I would prefer to handle the calculation on the application side (or wherever the result of the query is used) as I’m no fan of queries which reference another row of itself.
But a possible solution could be the use of a subselect where the date is used for the join.
As no DBMS was tagged I’ve used PostgreSQL 15.
Expect for the date calcuation for the join it should be useable with most DBMS.
The left join is necessary, so that the latest date will still appear (with admission_count_change null).
I expect this query to scale not so nice with a large table.
db<>fiddle to try it out yourself