Calculating patient census by hour - sql-server-2008

I am trying to build a query that calculates number of patients in the emergency room by hour. I have each patients arrival and departure times. I tried building a boolean style query but all it did was give me the arrivals by hour using this logic
SELECT MRN,
,CASE WHEN CAST(EDArrival AS TIME) between '00:00:00.000' and '00:59:59.000' then 1 else 0 end as Hour0
,CASE WHEN CAST(EDArrival AS TIME) between '01:00:00.000' and '01:59:59.000' then 1 else 0 end as Hour1
,CASE WHEN CAST(EDArrival AS TIME) between '02:00:00.000' and '02:59:59.000' then 1 else 0 end as Hour2
FROM EDArrivals
WHERE EDArrival between '2012-06-01' and '2013-07-01'
I was thinking maybe the query could place a column for each hour with a 1 or 0 in they were in the ED during those hours. What I ultimately want to get to is average patients in the ED by hour over the course of a year. If anyone can think of an easier method I would greatly appreciate the help.
Thank you

This probably won't perform great, but it will give the average for each hour over the time span you specify. The perf issue will be because of the function in the JOIN criteria in the CTE. If you need to do this for a very large number of rows it probably makes sense to break that out to another table and populate a column with the hour.
DECLARE #Hours TABLE (Hr smallint)
INSERT INTO #Hours
(Hr)
VALUES
(0)
,(1)
,(2)
,(3)
,(4)
,(5)
,(6)
,(7)
,(8)
,(9)
,(10)
,(11)
,(12)
,(13)
,(14)
,(15)
,(16)
,(17)
,(18)
,(19)
,(20)
,(21)
,(22)
,(23)
WITH ByDate
AS
(
SELECT
CAST(ED.EDArrival AS date) AS 'Dt',h.Hr, COUNT(*) AS 'PatientCount'
FROM
EDArrivals ED
JOIN
#Hours AS h
ON DATEPART(HOUR, ED.EDArrival) = h.Hr
WHERE
ED.EDArrival BETWEEN '2012-06-01' AND '2013-07-01'
GROUP BY
CAST(ED.EDArrival AS date)
,h.Hr
)
SELECT
Hr, AVG(PatientCount)
FROM
ByDate
GROUP BY
hr
ORDER BY
hr
I should also note that though you don't list it in your requirements, it probably makes more sense to also filter on the departure time is >= the given hour. You likely need to know not just how many patients show up but how many are sticking around at any given time.

I managed to create an example of my comment in SQLFiddle.
http://sqlfiddle.com/#!6/5234e/6
It's similar to JNK answer (hey, I commented first!)
By the way, creating that table variable will not be great, consider keeping a domain table with the hours.
If do you need performance consider also persisting the date part values. Evaluating them for each row is a performance killer.
Also take care with null departures date times and patients staying at midnight.

Have you tried using DateDiff:
SELECT DateDiff(n, startdate, enddate) FROM MyTable

SELECT COUNT(*) [TotalArrivals]
, DATEPART(hh, [EDArrival]) [Hour]
FROM [EDArrivals]
GROUP BY DATEPART(hh, [EDArrival])
This will get you the total arrivals grouped by hour. You can then use this to do your averages per hour / whatever other calculations you need. This wont give you the hours with no arrivals, but that should be easy to fit in to your calculations at the end.

Related

Query with three tables, no common column

I've just started a job and my boss wants me to learn mySQL so please bear with me, i've been learning for only 2 days and i'm not that good at it yet.
So i've been given 3 tables and several tasks to do.
The tables are:
mobile_log_messages_sms
mobile_providers
service_instances
And in them i've got to:
Find out how many messages there were in the last 25 days and how
much income did they make
Then i need to group them by day (so per day, exclude hours) and
provider name.
Also i need to ignore all the messages that have an empty string
under the service column
Also i need to ignore the messages that made 0 income and count only
those that have the column service_enabled = 1
And then i need to sort it descending, by date.
in the tables
mobile_log_messages_sms:
message_id - used to count the messages
price - using for price obviously, exlude those with 0
time - date in yyyy/mm/dd hh:mm:ss format
service - exclude all those that have an empty string (or null)
mobile_providers
provider_name - to use to group with
service_instances
enabled - only use if value is 1
I've started with:
SELECT message_id, price, time
FROM mobile_log_messages_sms
WHERE time BETWEEN '2017-02-26 00:00:00'
AND time AND '2017-03-22 00:00:00'
But i need to change the date format and then use the JOIN commands but i don't know how, and i know i need to add more to it, but i'm stumped even at the start. Also the starting just lists the messages but i need to count the total sum of the income (price) per day.
Can anyone point me in the right direction at least since i'm still a noob? Many thanks in advance and sorry if i worded something badly, english is not my first language.
Find out how many messages there were in the last 25 days and how much income did they make
1.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE;
2.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE
GROUP BY CAST(time AS DATE);
3.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE AND service IS NULL
GROUP BY CAST(time AS DATE);
rest can't done with join so make sure that at least one column should be common in tables.

MySQL How to join multiple sets of data and columns into a Single table

I am having trouble understanding the structure of the query i wish to perform. What i have is a large set of data in a table with multiple UnitID's. The units have temperatures and Timestamps of when the temperatures where recorded.
I want to be able to display the data where I can see the Average temperature of each unit separated in a weekly interval.
Apologies for my previous post, I'm still a novice with querying. But i will show you what i have done so far.
SELECT UnitID AS 'Truck ID',
AVG(Temp) As 'AVG Temp',
LogTime AS 'Event Time',
DAY(g.`LogTime`) as 'Day',
MONTH(g.`LogTime`) as 'Month',
COUNT(*) AS 'Count'
FROM `temperature` as g
WHERE DATE_SUB(g.`LogTime`,INTERVAL 1 WEEK)
AND Ana > 13 AND Ana < 16 AND NOT g.Temp = -100
GROUP BY 'truck id', YEAR(g.`LogTime`),MONTH(g.`LogTime`),WEEK(g.`LogTime`)
Order BY 'truck id', YEAR(g.`LogTime`),MONTH(g.`LogTime`),WEEK(g.`LogTime`)
;
(Sorry, I don't know how to display a table result at the moment)
This result gives me the weekly temperature averages of a truck, and shows me on which day of the month the temperature was recorded, as well as a count of temperatures per week, per truck.
The Query I want to perform , creates 5 columns, being UnitID, Week1, Week2, Week3, Week4.
Within the 'Week' columns I want to be able to display a weekly(Every day of the Week) temperature average for each truck, where the following week is set a week after the previous week (ie. Week2 is set to display the avg(temp) one week from Week1).
And this is where I am stuck on the structure of how to create the query. Im not sure if i need to create sub-queries or use a Union clause. I have tried a couple of queries , but i have deleted them because they did not work. I'm not sure if this query is too complex or if its even possible.
If anyone will be able to help I would greatly appreciate it. If there is any other info I can supply that will help, I will try to do so.
Hopefully this is solvable. :p
MySQL has a WEEK function that will return the week of the year as an integer (0-52). You can use that in you GROUP BY clause, and then use the AVG aggregation function to get the average temperature. Your query would look something like this:
SELECT unitID, WEEK(dateColumn) AS week, AVG(tempColumn) AS averageTemperature
FROM myTable
GROUP BY unitID, WEEK(dateColumn);
Here is a list of other helpful Date and Time Functions that may be useful for querying your database.

MySQL cumulative sum grouped by date

I know there have been a few posts related to this, but my case is a little bit different and I wanted to get some help on this.
I need to pull some data out of the database that is a cumulative count of interactions by day. currently this is what i have
SELECT
e.Date AS e_date,
count(e.ID) AS num_interactions
FROM example AS e
JOIN example e1 ON e1.Date <= e.Date
GROUP BY e.Date;
The output of this is close to what I want but not exactly what I need.
The problem I'm having is the dates are stored with the hour minute and second that the interaction happened, so the group by is not grouping days together.
This is what the output looks like.
On 12-23 theres 5 interactions but its not grouped because the time stamp is different. So I need to find a way to ignore the timestamp and just look at the day.
If I try GROUP BY DAY(e.Date) it groups the data by the day only (i.e everything that happened on the 1st of any month is grouped into one row) and the output is not what I want at all.
GROUP BY DAY(e.Date), MONTH(e.Date) is splitting it up by month and the day of the month, but again the count is off.
I'm not a MySQL expert at all so I'm puzzled on what i'm missing
New Answer
At first, I didn't understand you were trying to do a running total. Here is how that would look:
SET #runningTotal = 0;
SELECT
e_date,
num_interactions,
#runningTotal := #runningTotal + totals.num_interactions AS runningTotal
FROM
(SELECT
DATE(eDate) AS e_date,
COUNT(*) AS num_interactions
FROM example AS e
GROUP BY DATE(e.Date)) totals
ORDER BY e_date;
Original Answer
You could be getting duplicates because of your join. Maybe e1 has more than one match for some rows which is inflating your count. Either that or the comparison in your join is also comparing the seconds, which is not what you expect.
Anyhow, instead of chopping the datetime field into days and months, just strip the time from it. Here is how you do that.
SELECT
DATE(e.Date) AS e_date,
count(e.ID) AS num_interactions
FROM example AS e
JOIN example e1 ON DATE(e1.Date) <= DATE(e.Date)
GROUP BY DATE(e.Date);
I figured out what I needed to do last night... but since I'm new to this I couldn't post it then... what I did that worked was this:
SELECT
DATE(e.Date) AS e_date,
count(e.ID) AS num_daily_interactions,
(
SELECT
COUNT(id)
FROM example
WHERE DATE(Date) <= e_date
) as total_interactions_per_day
FROM example AS e
GROUP BY e_date;
Would that be less efficient than your query? I may just do the calculation in python after pulling out the count per day if its more efficient, because this will be on the scale of thousands to hundred of thousands of rows returned.

Overtime in MySQL timecard over two weeks

I'm trying to figure out how to calculate the overtime on timecard bills that have been or need to be paid. The problem is that bills cover more than one weeks worth of hours and the query grabs more than one employee's history at a time. Any suggestions on how to do this, perhaps with a case statement of some kind?
For example, suppose in my list of employees one works 39 hours one week and 45 the next. The bill would show 84 hours worked, and would also need to show 5 hours of overtime (not four!). This needs to be done in the context of the below query, which handles multiple bills and multiple employees.
Note the query below shows how this would work if the billing period was only one week.
select
username,
CASE
WHEN paidOn IS NULL THEN 'Unpaid'
ELSE paidOn
END as paid,
round(sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600,2) AS hours
, CASE
WHEN round(sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600,2) > 40
THEN round((sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600 - 40) * payrate + 40 * payrate,2)
ELSE
round(sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600 * payrate, 2)
END as pay
from
timecard
LEFT JOIN
employees
ON
employees.userID = timecard.userID
WHERE
paid != 'd'
GROUP BY
paidOn, timecard.userID
ORDER BY
paid DESC
LIMIT 30;
The way I understand it is that this works fine if the billing period is only 1 week, but not when you extend it to multiple weeks. Then I would simply use the query you have above as a subquery and then aggregate your weeks together. Here's a short example of what I mean:
SELECT
EmployeeId,
SUM(RegularPayHours) * RegularPayRate,
SUM(OverTimeHours) * OverTimeRate
FROM
(SELECT
EmployeeId,
DATEPART(week,TimeCardDate) AS [WorkWeek],
CASE WHEN SUM(HoursWorked) > 40 THEN 40 ELSE SUM(HoursWorked) END AS [RegularPayHours],
CASE WHEN SUM(HoursWorked) > 40 THEN SUM(HoursWorked) - 40 ELSE 0 END AS [OvertimeHours]
FROM
TimeCard
WHERE
TimeCardDate BETWEEN StartDate AND EndDate
GROUP BY
EmployeeId,
DATEPART(week,TimeCardDate)
) a
WHERE
WorkWeek IN (1,2)
GROUP BY
EmployeeId
This will give you the first two work weeks combined as one result but with the overtime calculations done at the week level. You can essentially create any custom pay period that is X weeks long with this method.
You may need to custom define what a work-week is by setting the DATEFIRST value.

Group by date from multiple columns?

first of all sorry for that title, but I have no idea how to describe it:
I'm saving sessions in my table and I would like to get the count of sessions per hour to know how many sessions were active over the day. The sessions are specified by two timestamps: start and end.
Hopefully you can help me.
Here we go:
http://sqlfiddle.com/#!2/bfb62/2/0
While I'm still not sure how you'd like to compare the start and end dates, looks like using COUNT, YEAR, MONTH, DAY, and HOUR, you could come up with your desired results.
Possibly something similar to this:
SELECT COUNT(ID), YEAR(Start), HOUR(Start), DAY(Start), MONTH(Start)
FROM Sessions
GROUP BY YEAR(Start), HOUR(Start), DAY(Start), MONTH(Start)
And the SQL Fiddle.
What you want to do is rather hard in MySQL. You can, however, get an approximation without too much difficulty. The following counts up users who start and stop within one day:
select date(start), hour,
sum(case when hours.hour between hour(start) and hours.hour then 1 else 0
end) as GoodEstimate
from sessions s cross join
(select 0 as hour union all
select 1 union all
. . .
select 23
) hours
group by date(start), hour
When a user spans multiple days, the query is harder. Here is one approach, that assumes that there exists a user who starts during every hour:
select thehour, count(*)
from (select distinct date(start), hour(start),
(cast(date(start) as datetime) + interval hour(start) hour as thehour
from sessions
) dh left outer join
sessions s
on s.start <= thehour + interval 1 hour and
s.end >= thehour
group by thehour
Note: these are untested so might have syntax errors.
OK, this is another problem where the index table comes to the rescue.
An index table is something that everyone should have in their toolkit, preferably in the master database. It is a table with a single id int primary key indexed column containing sequential numbers from 0 to n where n is a number big enough to do what you need, 100,000 is good, 1,000,000 is better. You only need to create this table once but once you do you will find it has all kinds of applications.
For your problem you need to consider each hour and, if I understand your problem you need to count every session that started before the end of the hour and hasn't ended before that hour starts.
Here is the SQL fiddle for the solution.
What it does is use a known sequential number from the indextable (only 0 to 100 for this fiddle - just over 4 days - you can see why you need a big n) to link with your data at the top and bottom of the hour.