Average number of records per hour per datetime range

  • Hi,
    I have a DB with PATIENT_ID, ARRIVAL_DATE, DEPARTURE_DATE, and DEPARTMENT. I need to find the average number of patients in each department per hour over a time frame. So if the date range chosen is 01/01/2018, and the end date chosen is 02/01/2018, then I need to know the average number of patients that were in each department in hours 1:00 to 23:00 over this time period. I have tried several ways but I cannot get this to work Any help would be greatly appreciated!

  • robert693 - Wednesday, February 14, 2018 1:39 PM

    Hi,
    I have a DB with PATIENT_ID, ARRIVAL_DATE, DEPARTURE_DATE, and DEPARTMENT. I need to find the average number of patients in each department per hour over a time frame. So if the date range chosen is 01/01/2018, and the end date chosen is 02/01/2018, then I need to know the average number of patients that were in each department in hours 1:00 to 23:00 over this time period. I have tried several ways but I cannot get this to work Any help would be greatly appreciated!

    Sounds straightforward. You'll get far more responses if you script out some sample data, in consumable format.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Phil Parkin - Wednesday, February 14, 2018 1:43 PM

    robert693 - Wednesday, February 14, 2018 1:39 PM

    Hi,
    I have a DB with PATIENT_ID, ARRIVAL_DATE, DEPARTURE_DATE, and DEPARTMENT. I need to find the average number of patients in each department per hour over a time frame. So if the date range chosen is 01/01/2018, and the end date chosen is 02/01/2018, then I need to know the average number of patients that were in each department in hours 1:00 to 23:00 over this time period. I have tried several ways but I cannot get this to work Any help would be greatly appreciated!

    Sounds straightforward. You'll get far more responses if you script out some sample data, in consumable format.

    In the meantime, checkout DATEPART in books online. That and GROUP BY will be in your solution I think.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • TheSQLGuru - Wednesday, February 14, 2018 3:00 PM

    Phil Parkin - Wednesday, February 14, 2018 1:43 PM

    robert693 - Wednesday, February 14, 2018 1:39 PM

    Hi,
    I have a DB with PATIENT_ID, ARRIVAL_DATE, DEPARTURE_DATE, and DEPARTMENT. I need to find the average number of patients in each department per hour over a time frame. So if the date range chosen is 01/01/2018, and the end date chosen is 02/01/2018, then I need to know the average number of patients that were in each department in hours 1:00 to 23:00 over this time period. I have tried several ways but I cannot get this to work Any help would be greatly appreciated!

    Sounds straightforward. You'll get far more responses if you script out some sample data, in consumable format.

    In the meantime, checkout DATEPART in books online. That and GROUP BY will be in your solution I think.

    Can't we use normal conventional Group by here ?

  • subramaniam.chandrasekar - Thursday, February 15, 2018 6:36 AM

    TheSQLGuru - Wednesday, February 14, 2018 3:00 PM

    Phil Parkin - Wednesday, February 14, 2018 1:43 PM

    robert693 - Wednesday, February 14, 2018 1:39 PM

    Hi,
    I have a DB with PATIENT_ID, ARRIVAL_DATE, DEPARTURE_DATE, and DEPARTMENT. I need to find the average number of patients in each department per hour over a time frame. So if the date range chosen is 01/01/2018, and the end date chosen is 02/01/2018, then I need to know the average number of patients that were in each department in hours 1:00 to 23:00 over this time period. I have tried several ways but I cannot get this to work Any help would be greatly appreciated!

    Sounds straightforward. You'll get far more responses if you script out some sample data, in consumable format.

    In the meantime, checkout DATEPART in books online. That and GROUP BY will be in your solution I think.

    Can't we use normal conventional Group by here ?

    Yes, but you'll need some way to determine the groups and DATEPART is one method of determining the groups.  I would actually use a slightly different method which essentially rounds down to the nearest whole hour.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • subramaniam.chandrasekar - Thursday, February 15, 2018 6:36 AM

    TheSQLGuru - Wednesday, February 14, 2018 3:00 PM

    In the meantime, checkout DATEPART in books online. That and GROUP BY will be in your solution I think.

    Can't we use normal conventional Group by here ?

    I see no mention of using an unconventional GROUP BY, so I don't understand the reason for your comment.

    If the underlying date data types contain time information – and we must assume that they do, based on the requirement – then DATEPART can be used to break these datetimes out into hourly blocks, before grouping them.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • The query should have a parameter with a start date and an end date. Arrival Date and Departure date are DateTime fields. Between the start date and the end date, the query should show the number of PATIENT_IDs per hour. So if the record of the PATIENT_ID had an ARRIVAL_DATE of 01/16/2018 04:23:00 am and a  DEPARTURE_DATE of 01/18/2018 12:31:00 pm, then in any given hour between 01/16/2018 04:23:00 am and 01/18/2018 12:31:00 pm this PATIENT_ID would count a 1. From the start date to the end date I need,per hour, to find the average number of PATIENT_IDs PRESENT.

  • Phil Parkin - Thursday, February 15, 2018 7:20 AM

    subramaniam.chandrasekar - Thursday, February 15, 2018 6:36 AM

    TheSQLGuru - Wednesday, February 14, 2018 3:00 PM

    In the meantime, checkout DATEPART in books online. That and GROUP BY will be in your solution I think.

    Can't we use normal conventional Group by here ?

    I see no mention of using an unconventional GROUP BY, so I don't understand the reason for your comment.

    If the underlying date data types contain time information – and we must assume that they do, based on the requirement – then DATEPART can be used to break these datetimes out into hourly blocks, before grouping them.

    It was actually a wrong ping / comment to your quote, Apologies. thought of replying OP:hehe:

  • robert693 - Thursday, February 15, 2018 7:25 AM

    The query should have a parameter with a start date and an end date. Arrival Date and Departure date are DateTime fields. Between the start date and the end date, the query should show the number of PATIENT_IDs per hour. So if the record of the PATIENT_ID had an ARRIVAL_DATE of 01/16/2018 04:23:00 am and a  DEPARTURE_DATE of 01/18/2018 12:31:00 pm, then in any given hour between 01/16/2018 04:23:00 am and 01/18/2018 12:31:00 pm this PATIENT_ID would count a 1. From the start date to the end date I need,per hour, to find the average number of PATIENT_IDs PRESENT.

    OK, so based on the advice you've received so far, what have you tried?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • robert693 - Wednesday, February 14, 2018 1:39 PM

    Hi,
    I have a DB with PATIENT_ID, ARRIVAL_DATE, DEPARTURE_DATE, and DEPARTMENT. I need to find the average number of patients in each department per hour over a time frame. So if the date range chosen is 01/01/2018, and the end date chosen is 02/01/2018, then I need to know the average number of patients that were in each department in hours 1:00 to 23:00 over this time period. I have tried several ways but I cannot get this to work Any help would be greatly appreciated!

    Please see the following article with the understanding that you would apply the method to hours instead of months.
    http://www.sqlservercentral.com/articles/T-SQL/105968/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply