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 9162293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:10:33+00:00 2026-06-17T14:10:33+00:00

here is what I am dealing with: I am given a table (Visit) with

  • 0

here is what I am dealing with:
I am given a table (Visit) with the following schema with sample data.

VISIT

VisitNo   Location  AdmissionDate  DischargeDate LengthOfStay
1         A         2012-04-28     2012-05-30     32
2         A         2012-04-20     2013-05-20     90
3         B         2012-04-01     2012-05-01     30
4         B         2012-05-01     2012-05-03     2
.....................................................

The requirements is that the returned data set is in the following structure. Total LengthOfStay of each location by Calendar Month(YYYYMM).

CalendarMonth  TotalLengthOfStayEachMonth(AdmissionToDate)  Location
201204          xxx                                          x
201205          yyyy                                         y
201206          zzzz                                         z
.........      ...............

The calculation of TotalLengthOfStayEachMonth is a little tricky. The days are counted since the AdmissionDate (admissionToDate), not literally 30 days per month basis.
For example, The first record in the VISIT table has;

  • 2 days of stay in 201204
  • 2+30 = 32 of days in 201205 and so forth…

Thanks in advance for your suggestions… FYI, we have hundreds of locations, thousand of vists and 5 years of data.

  • 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-17T14:10:35+00:00Added an answer on June 17, 2026 at 2:10 pm

    While I’d prefer seeing your desired output, perhaps something like this will help get you started:

    SELECT 
        CONVERT(char(6), AdmissionDate, 112)  as CalendarMonth  ,
        SUM(DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,AdmissionDate)+1,0)))-DAY(AdmissionDate)) as TotalLengthOfStayEachMonth,
        Location
    FROM VISIT
    GROUP BY CONVERT(char(6), AdmissionDate, 112), Location
    

    Adding the Location will possibly give you duplicate CalendarMonths, but I assume that’s ok.

    EDIT —

    I played around with this a little more as I realized some months would be missing dates if the DischargeDate was greater than 1 month over the AdmissionDate, and came up with this — it uses the spt_values table:

    select 
       CONVERT(char(6), AdmissionDate+v.number, 112)  as CalendarMonth,
       COUNT(*) Days,
       Location
    from Visit t
    inner join master..spt_values v
      on v.type='P' and v.number <= DATEDIFF(d, AdmissionDate, DischargeDate)
    group by Location, CONVERT(char(6), AdmissionDate+v.number, 112)
    order by CONVERT(char(6), AdmissionDate+v.number, 112), Location
    

    Here is the SQL Fiddle.

    — Another Edit

    While reviewing another answer, and not really understanding your situation (need that desired output), if your desired output is 2 days in April for your first record, then you just need to add and v.number <> 0 to the join above — easy enough, just wasn’t sure what you were requesting. This should be the simplest solution:

    select 
       CONVERT(char(6), AdmissionDate+v.number, 112)  as CalendarMonth,
       COUNT(*) Days,
       Location
    from Visit t
    inner join master..spt_values v
      on v.type='P' and v.number <= DATEDIFF(d, AdmissionDate, DischargeDate) and v.number <> 0
    group by Location, CONVERT(char(6), AdmissionDate+v.number, 112)
    order by CONVERT(char(6), AdmissionDate+v.number, 112), Location
    

    More fiddle.

    Hope some of this helps.

    Good luck.

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

Sidebar

Related Questions

I am dealing with a MySQL table here that is keyed in a somewhat
Here's a screenshot of what I'm dealing with: As you can see, the spacing
First days dealing with Perl and blocked already :) Here's the situation: a file
I'm dealing with some gnarly HTML. Here's a stylized example of the ancestry of
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Little help here: I am uploading my site following the instructions on this link
Here's the situation I'm dealing with. The original query I had was: SELECT c.CustID,
I can't find this piece of information. I'm dealing with an odd situation here
I am dealing with a problem of text summarization i.e. given a large chunk(s)
Given the below program, I am having issues dealing with monads. module Main where

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.