Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Average Daily Census

Posted on 2015-01-23
12
Medium Priority
?
93 Views
Last Modified: 2015-12-05
I have records that have start and end dates, denoting a clients' length of stay.  Say client 1 started on 1/2/2015 and ended on 1/5/2015.  Client 2 started 1/3/2015 and ended 1/6/2015, client 3 started... etc...
I need to report census by day:
1/1/2015 - 0
1/2/2015 - 1
1/3/2015 - 2
etc...

What is the cleanest way to do this.  I am using SQL Server 2008 r2.
0
Comment
Question by:UniqueData
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 40566863
A "calendar" is best for something like this:

CREATE TABLE tblPatientStays (ID int identity(1, 1), PatientID int, StayStart datetime, StayEnd datetime)

INSERT INTO tblPatientStays (PatientID, StayStart, StayEnd) VALUES
    (1, '2015-01-02', '2015-01-05'),
    (2, '2015-01-04', '2015-01-10')

CREATE TABLE tblCalendar(Dt datetime)

INSERT INTO tblCalendar (Dt) VALUES
    ('2015-01-01'),
    ('2015-01-02'),
    ('2015-01-03'),
    ('2015-01-04'),
    ('2015-01-05'),
    ('2015-01-06'),
    ('2015-01-07'),
    ('2015-01-08'),
    ('2015-01-09'),
    ('2015-01-10'),
    ('2015-01-11'),
    ('2015-01-12'),
    ('2015-01-13'),
    ('2015-01-14'),
    ('2015-01-15'),
    ('2015-01-16'),
    ('2015-01-17'),
    ('2015-01-18'),
    ('2015-01-19'),
    ('2015-01-20'),
    ('2015-01-21'),
    ('2015-01-22'),
    ('2015-01-23'),
    ('2015-01-24'),
    ('2015-01-25'),
    ('2015-01-26'),
    ('2015-01-27'),
    ('2015-01-28'),
    ('2015-01-29'),
    ('2015-01-30'),
    ('2015-01-31')
    
SELECT c.Dt, COUNT(p.PatientID) AS NumPatients
FROM tblCalendar c LEFT JOIN
    tblPatientStays p ON c.Dt >= p.StayStart AND c.Dt <= p.StayEnd
GROUP BY c.Dt
ORDER BY c.Dt

DROP TABLE tblCalendar
DROP TABLE tblPatientStays

Open in new window

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 40566931
There is also a quick and dirty method that exploits one of the system tables, but the calendar table approach is definitely better.

CREATE TABLE tblPatientStays (ID int identity(1, 1), PatientID int, StayStart datetime, StayEnd datetime)

INSERT INTO tblPatientStays (PatientID, StayStart, StayEnd) VALUES
    (1, '2015-01-02', '2015-01-05'),
    (2, '2015-01-04', '2015-01-10')

SELECT c.Dt, COUNT(p.PatientID) AS NumPatients
FROM (
    SELECT DATEADD(day, number, '2015-01-01') AS Dt
    FROM master..spt_values
    WHERE Type = 'P' AND number < 31) c LEFT JOIN
    tblPatientStays p ON c.Dt >= p.StayStart AND c.Dt <= p.StayEnd
GROUP BY c.Dt
ORDER BY c.Dt

DROP TABLE tblPatientStays

Open in new window

0
 
LVL 7

Author Comment

by:UniqueData
ID: 40566951
What is wrong with the second option?  Why is the first better?  The reason I ask is I am going to run this for a full year (or possibly more). Although I don't quite understand what is happening :)  the second looks cleaner and you don't seem to hard code any dates.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 7

Author Comment

by:UniqueData
ID: 40566982
Ah, I see.  That system table just has values when type = 'P'.  So I would have to adjust the 'where < 32 ' depending on the number of days in each month
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40567000
If you have two known dates, just use DATEDIFF.  Absolutely no reason to waste resource using a calendar table and aggregation of top of it for that:

SELECT ..., DATEDIFF(DAY, start_date, end_date) + 1 AS total_stay_in_days, ...
FROM ...
0
 
LVL 7

Author Comment

by:UniqueData
ID: 40567033
Sorry Scott, Patrick is on the right track as that is how they currently calculate average daily census by month.  Getting length of stay alone and averaging will only get you average length of stay, not daily/monthly averages.

But Patrick, any ideas on how to span multiple months or if a client's stay goes over two months?
0
 
LVL 7

Author Comment

by:UniqueData
ID: 40567077
I was able to tweak Patrick's code to make it a little more flexible, but not sure how to handle multiple months...

CREATE TABLE tblPatientStays (ID int identity(1, 1), PatientID int, StayStart datetime, StayEnd datetime)

INSERT INTO tblPatientStays (PatientID, StayStart, StayEnd) VALUES
    (1, '2015-01-02', '2015-02-05'),
    (2, '2015-01-04', '2015-01-10')
--Get First of month: DATEADD(month, DATEDIFF(month, 0, @mydate), 0)  
SELECT c.Dt, COUNT(p.PatientID) AS NumPatients
FROM (
    SELECT DATEADD(day, number, DATEADD(month, DATEDIFF(month,0,(Select Min(StayStart) From tblPatientStays)), 0)) AS Dt
    FROM master..spt_values
    WHERE Type = 'P' 
			AND number < DatePart(day,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,(Select Min(StayStart) From tblPatientStays))+1,0)))
	) c LEFT JOIN
    tblPatientStays p ON c.Dt between p.StayStart AND p.StayEnd
GROUP BY c.Dt
ORDER BY c.Dt

DROP TABLE tblPatientStays

Open in new window

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40567094
"its quick and dirty"

it uses a system table that in the end you have no control over,

that means it could change as the result of any patch/fix and version release....
or your use of it could start to impact system performance....

for a quick one-off test in a development system , its a quick trick dba's sometimes use to look at things with...

as production level code its better to implement your own solutions ....
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40567096
A tally table is still perfectly fine.  You don't need a "calendar" table for this.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 40567231
The problem with the spt_values approach is that it's kind of a "hidden" feature: not documented, and there is always the possibility that Microsoft kills it in a future release.  Also, you're limited to 2047 items, so you couldn't use this to generate, say, a daily calendar over 10 years.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40567298
You can also have  permissions issues if the user running the code isn't allowed to read the *master*.dbo.spt_values table.
0
 
LVL 7

Author Comment

by:UniqueData
ID: 40587987
I still don't undersrtand how I can handle multiple months.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question