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!
Assuming a data structure like that:
You could query the number of occupied rooms per hour like that:
to_date('2008-09-19','yyyy-mm-dd')is the start date for your query,168the number of hours you want reported.EDIT: To get the maximum number and the lastest date with that number, use