Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:58:23+00:00 2026-06-18T14:58:23+00:00

I have hospital patient admission data in Microsoft SQL Server r2 that looks something

  • 0

I have hospital patient admission data in Microsoft SQL Server r2 that looks something like this:

PatientID, AdmitDate,        DischargeDate
Jones.     1-jan-13 01:37.   1-jan-13 17:45
Smith      1-jan-13 02:12.   2-jan-13 02:14
Brooks.    4-jan-13 13:54.   5-jan-13 06:14

I would like count the number of patients in the hospital day by day and hour by hour (ie at

1-jan-13 00:00. 0
1-jan-13 01:00. 0
1-jan-13 02:00. 1
1-jan-13 03:00. 2

And I need to include the hours when there are no patients admitted in the result.

I can’t create tables so making a reference table listing all the hours and days is out, though.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:58:24+00:00Added an answer on June 18, 2026 at 2:58 pm

    To solve this problem, you need a list of date-hours. The following gets this from the admit date cross joined to a table with 24 hours. The table of 24 hours is calculating from information_schema.columns — a trick for getting small sequences of numbers in SQL Server.

    The rest is just a join between this table and the hours. This version counts the patients at the hour, so someone admitted and discharged in the same hour, for instance is not counted. And in general someone is not counted until the next hour after they are admitted:

    with dh as (
         select DATEADD(hour, seqnum - 1, thedatehour ) as DateHour
         from (select distinct cast(cast(AdmitDate as DATE) as datetime) as thedatehour
               from Admission a
              ) a cross join
              (select ROW_NUMBER() over (order by (select NULL)) as seqnum
               from INFORMATION_SCHEMA.COLUMNS
              ) hours
              where hours <= 24
        )
    select dh.DateHour, COUNT(*) as NumPatients
    from dh join
         Admissions a
         on dh.DateHour between a.AdmitDate and a.DischargeDate
    group by dh.DateHour
    order by 1
    

    This also assumes that there are admissions on every day. That seems like a reasonable assumption. If not, a calendar table would be a big help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of hospital admission data that I need to process, I
In SQL Server 2005 I have a table that stores incoming and outgoing patients
I have data of about hospital i.e. its patient details,location. Now I have to
I have a hospital visit data that contain records for gender, age, main diagnosis,
I have a DataTable which looks like below: TOTAL_CODE COD_NAME AP0001 School AP0002 Hospital
Any help here would be greatly appreciated. I have this table hospital Nurse |
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I’m doing a patient registration system to a hospital, here we have 1st time
I have some hospital data in a dataframe, read in from a csv. I
I have in my php a request that get hospital patients information, in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.