I am currently trying to write a query to calculate patient days. My source table has VisitID, AdmitDateTime, and DischargeDateTime. A patient day is defined as a patient is in a bed at midnight. So for example, if there were 5 patients admitted on 2016-01-01 and no other patients were in the hospital from before then Patient Days would equal 5 for 2016-01-02.
I would like to display the results with the columns being date and count. I thought about building a calendar table with a CTE but I'm unsure of what the join should be to my source table. If there are no patients in a bed for a certain day I would want the count to be 0. I'm fairly new to SQL any and all help would be greatly appreciated.