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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:44:39+00:00 2026-05-27T22:44:39+00:00

I have a requirement to calculate summary statistics aggregated by specific custom time periods.

  • 0

I have a requirement to calculate summary statistics aggregated by specific custom time periods. Specifically, a restaurant chain is open 24 hours a day. I need to calculate statistics like total sales by period, where periods are “Breakfast”, “Lunch”, “Dinner” and “Overnight”. For this company, the official day for which they track statistics begins after dinner. So the 24 hour period that consititutes an official day starts at 8PM and runs until 8PM CST) the next day. That is one period. Another period is “Overnight” which runs from 8PM to 5:30AM. I put these definitions into a table called “tdef” like so:

drop table tdef cascade constraints 
;

create table tdef 
(
    cd char(3) not null,
    start_ts date not null,
    stop_ts date not null 
)

And then I insert the definitions into the tdef table, stored as dates where the start date always begins on Jan 1 1900, and if it spans across midnight, then it ends on Jan 2 1900. Like so,

insert into tdef (start_ts, stop_ts, cd) 
values
(
to_date('1900/01/01 20:00:00', 'yyyy/mm/dd hh24:mi:ss'),
to_date('1900/01/02 19:59:59', 'yyyy/mm/dd hh24:mi:ss'),
'24H'
);

insert into tdef (start_ts, stop_ts, cd) 
values
(
to_date('1900/01/01 10:30:00', 'yyyy/mm/dd hh24:mi:ss'),
to_date('1900/01/01 13:29:59', 'yyyy/mm/dd hh24:mi:ss'),
'LUN
);

insert into tdef (start_ts, stop_ts, cd) 
values
(
to_date('1900/01/01 15:30:00', 'yyyy/mm/dd hh24:mi:ss'),
to_date('1900/01/02 08:29:59', 'yyyy/mm/dd hh24:mi:ss'),
'ON'
);

I have a very large table (about 2.5 billion rows) which contains all register transactions. I need to summarize sales by date (their defintion of 8PM-8PM), product and time dimension and store this in a table for fast access reporting. The table should look like this:

Dec 12 2011, Hamburger, 24H, 1000
Dec 12 2011, Hamburger, ON, 100
Dec 12 2011, Hamburger, LUN, 400

Here is what I did to accomplish this, I added two date columns to the transaction table which are the time of the transaction on 1/1/1900 and 1/2/1900, like so:

to_date(concat('01/01/1900 ', tran_tm), 'mm/dd/yyyy hh24:mi'),
to_date(concat('01/02/1900 ', tran_tm), 'mm/dd/yyyy hh24:mi')

I indexed these two columns. Then I created a cross look up table that associated transaction ids with time codes. Each transaction code may be in more than one time defintion. So it looks like this:

24H, 1
24H, 2
24H, 3
...
LUN, 100
LUN, 101
LUN, 102
...
ON, 1
ON, 2
...

I used two insert select statements to accomplish this:

select  t.trans_id, td.cd, to_date(to_char(to_date(concat(to_char(ts, 'mm/dd/yyyy '), to_char(td.stop_ts, 'hh24:mi:ss')), 'mm/dd/yyyy hh24:mi:ss', 'yyyymmdd'), 'yyyymmdd')
from trans t, tdef td
where ts1 >= td.start_ts and ts1 <= td.stop_ts

select  t.trans_id, td.cd, to_date(to_char(to_date(concat(to_char(ts, 'mm/dd/yyyy '), to_char(td.stop_ts, 'hh24:mi:ss')), 'mm/dd/yyyy hh24:mi:ss', 'yyyymmdd'), 'yyyymmdd')
from trans t, tdef td
where ts2 >= td.start_ts and ts2 <= td.stop_ts

The third field is the “official date”. The way that this works, assume a transaction happened at 12/12/2011 8:01PM, then the ts1 field would be 1/1/1900 8:01PM and the ts2 field would be 1/2/1900 8:01PM. In the first query, this field would join to the cd ’24H’ and ‘ON’. And the official date would calcuate as 12/13/2011 for ’24H’ and 12/13/2011 for ‘ON’. This transaction would not join on the second query becuase it is outside the date range. Assume a transaction happened at 12/13/2011 12:05PM. On the first query, ts1 would join like so: ’24H’ for the date 12/13/2011, ‘LUN’ for the date 12/13/2011.

Once I have this table, it is easy to aggregate:

select tdef_trans.dt, sum(sales) from trans, tdef_trans where trans.id = tdef_trans.id and tdef_trans.cd = 'LUN'

Although this solution appears to be working, I am betting there is a more elegant way to do this. Any ideas?

  • 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-27T22:44:39+00:00Added an answer on May 27, 2026 at 10:44 pm

    If you are trying to do data warehousing (it sounds like it), then you may find it easiest to make a table that has every second of the day in it, and which period it belongs to. That will only be 86400 rows.

    Then your query becomes a relatively simple join to this time dimension

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

Sidebar

Related Questions

I have requirement, wherein I have to calculate totals for cash and credit cards
I have a requirement to calculate the average of a very large set of
I have a requirement for my new app, where i have to calculate Data
I have a requirement to start my application at certain time. I don’t want
I have a requirement to calculate the installed base for units with different placements/shipments
I have a requirement to calculate the Moving Range of a load of data
I got a new requirement from the business about real time data/statistics. They want
I have a requirement where i want to calculate last Date of a given
I'm having the requirement, to insert a specific amount of rectangles (which have a
My SQL is rusty -- I have a simple requirement to calculate the sum

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.