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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:13:35+00:00 2026-05-12T22:13:35+00:00

Im having a bit of a mental block on this one. I got booking

  • 0

Im having a bit of a mental block on this one.

I got booking system for hotel rooms and it contains a table as so

BookingRoomLink
BookingId (FK)
RoomId (FK)
Start_Date
End_Date

I’d like to query the data to extract occupancy levels for each month.
I could do this manually (ie for the past month do something like this).

 SELECT BookingRoomLink.Start_Date,
        BookingRoomLink.End_Date,
        DATEDIFF("d", BookingRoomLink.Start_Date, BookingRoomLink.End_Date) as RoomNights
   FROM BookingRoomLink 
  WHERE BookingRoomLink.Start_Date >= dateadd(m, -1, getdate()) 
    AND BookingRoomLink.End_Date <= GETDATE()

Then i can do a count on the results or similar which would give me the room nights “used” and subtract this against the room nights available in a month.

Eg. 10 rooms
x 30 days in the month = 300 possible room nights available. 150 used (result from query) = 50% occupancy.

The problem

Id like to automate this into a stored procedure.

Is it possible to group this into months for a given year?

How would I ensure that bookings which overlap a month boundry are suitable handled?

  • 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-05-12T22:13:35+00:00Added an answer on May 12, 2026 at 10:13 pm
    WITH    (
            SELECT  0
            UNION ALL
            SELECT  m + 1
            FROM    mon
            WHERE   m < 11
            ),
            yr (y) AS
            (
            SELECT  CAST('1990-01-01' AS DATETIME)
            UNION ALL
            SELECT  DATEADD(year, 1, y)
            FROM    yr
            WHERE   y <= GETDATE()
            ),
            dates (smy, emy) AS
            (
            SELECT  DATEADD(month, m, y), DATEADD(month, m + 1, y)
            FROM    yr
            CROSS JOIN
                    mon
            ),
            diffs (smy, emy, days) AS
            (
            SELECT  smy, emy, DATEDIFF(day, smy, emy)
            FROM    dates
            )
    SELECT  smy,
            roomId,
            CAST(SUM(DATEDIFF(day,
            CASE WHEN start_date < smy THEN smy ELSE start_date END,
            CASE WHEN end_date > emy THEN emy ELSE end_date END
            )) AS FLOAT) / days
    FROM    diffs
    JOIN    bookings
    ON      start_date < emy
            AND end_date >= smy
    GROUP BY
            roomId, smy, emy, days
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm having a bit of a mental block today/this week, so I'm probably
Having a bit of a mental block. I am sure I found a function
Having a bit of trouble wrapping my head around this one. I'm sure the
I know I've come across this problem before, but I'm having a bit of
I'm looking to crowd-source this, as I'm having bit of difficulty finding an industry
Having a bit of difficulty figuring out how to create a named_scope from this
Having a bit of trouble wrapping my head around Qtimer this morning. Here's the
having a bit of a prob with this, though I'm sure it's simple! I
having a bit of trouble finding a solution to this. I want to take
Having a bit of trouble with the application block, trying to figure out a

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.