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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:50:41+00:00 2026-05-25T16:50:41+00:00

Need some help in some SQL. I have the following report that I need

  • 0

Need some help in some SQL.

I have the following report that I need to build, but I ideally want to try and build it using an SQL Script, rather than having to have a procedure that runs every hour etc.

The business problem we are solving is basically calculating the maximum amount of locations that are occupied in a given week by products, calculated hourly.

I know I can do this by running a procedure every hour that counts the amount and inserts it into a table. I would then query this table at the end of the week and see which hour of which day had the greatest number.

Ideally I want to do this without using procedures. I have SQL written that can tell me the number at any point in time (between say the hours of 10-11AM on Monday).

Rather than copying and pasting this SQL script 24 x 7 times (1 for each hour of the day), is there something else I can do through SQL script here?
Could I create a maintenance table that has every day, and time period listed (e.g. columns would be: day, hour_start, hour_end), join that onto my query and use a max function?

I’m pretty sure that it can’t be done through strait SQL but I’m not a fan of time dependant procedures running (e.g. what if the server was to go offline).

Any advice appreciated!

  • 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-25T16:50:42+00:00Added an answer on May 25, 2026 at 4:50 pm

    Assuming a data structure like that:

    create table room_usage ( 
       roomnumber number(6),
       occupied_by varchar2(20),
       startdate date,
       enddate date
    );
    

    You could query the number of occupied rooms per hour like that:

    with datgen as 
      (select to_date('2008-09-19','yyyy-mm-dd')+(rownum-1)/24 d 
         from dual
         connect by rownum<=168)
    select d, (select count(*) from room_usage
                 where startdate<=datgen.d
                   and enddate>=datgen.d) occupied
      from datgen;
    

    to_date('2008-09-19','yyyy-mm-dd') is the start date for your query, 168 the number of hours you want reported.

    EDIT: To get the maximum number and the lastest date with that number, use

    with datgen as 
      (select to_date('2008-09-19','yyyy-mm-dd')+(rownum-1)/24 d 
         from dual
         connect by rownum<=168),
    occ_count as (     
      select d, (select count(*) from room_usage
                   where startdate<=datgen.d
                     and enddate>=datgen.d) occupied
        from datgen)
    select d, occupied from (select * from occ_count order by occupied desc, d desc)
     where rownum=1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help to build SQL Query. I have table having data like:
Need some help about with Memcache. I have created a class and want to
I need some help from the shell-script gurus out there. I have a .txt
I need some help writing a SQL statement. I have two tables in a
(Using SQL Server 2008) I need some help visualizing a solution. Let's say I
I need some help optimizing the following method. The queries have become too costly
I need some hibernate/SQL help, please. I'm trying to generate a report against an
I need some help with SQL Query. I am trying to select all records
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

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.