Suppose I have patient admission/claim wise data like the sample below. Data type of patient_id and hosp_id columns is VARCHAR
Table name claims
rec_no | patient_id | hosp_id | admn_date | discharge_date |
---|---|---|---|---|
1 | 1 | 1 | 01-01-2020 | 10-01-2020 |
2 | 2 | 1 | 31-12-2019 | 11-01-2020 |
3 | 1 | 1 | 11-01-2020 | 15-01-2020 |
4 | 3 | 1 | 04-01-2020 | 10-01-2020 |
5 | 1 | 2 | 16-01-2020 | 17-01-2020 |
6 | 4 | 2 | 01-01-2020 | 10-01-2020 |
7 | 5 | 2 | 02-01-2020 | 11-01-2020 |
8 | 6 | 2 | 03-01-2020 | 12-01-2020 |
9 | 7 | 2 | 04-01-2020 | 13-01-2020 |
10 | 2 | 1 | 31-12-2019 | 10-01-2020 |
I have another table wherein bed strength/max occupancy strength of hospitals are stored.
table name beds
hosp_id | bed_strength |
---|---|
1 | 3 |
2 | 4 |
Expected Results I want to find out hospital-wise dates where its declared bed-strength has exceeded on any day.
Code I have tried Nothing as I am new to SQL. However, I can solve this in R with the following strategy
pivot_longer
the datestidyr::complete()
missing dates in betweensummarise
oraggregate
results for each date.
Simultaneously, I also want to know that whether it can be done without pivoting (if any) in sql because in the claims
table there are 15 million + rows and pivoting really really slows down the process. Please help.