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

  • Home
  • SEARCH
  • 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 6364343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:09:06+00:00 2026-05-25T00:09:06+00:00

I have a database table that contains collection data for product collected from a

  • 0

I have a database table that contains collection data for product collected from a supplier and I need to produce an estimate of month-to-date production figures for that supplier using an Oracle SQL query. Each day can have multiple collections, and each collection can contain product produced across multiple days.

Here’s an example of the raw collection data:

Date        Volume  ColectionNumber  ProductionDays
2011-08-22  500     1                2
2011-08-22  200     2                2
2011-08-20  600     1                2

Creating a month-to-date estimate is tricky because the first day of the month may have a collection for two days worth of production. Only a portion of that collected volume is actually attributable to the current month.

How can I write a query to produce this estimate?

My gut feeling is that I should be able to create a database view that transforms the raw data into estimated daily production figures by summing collections on the same day and distributing collection volumes across the number of days they were produced on. This would allow me to write a simple query to find the month-to-date production figure.

Here’s what the above collection data would look like after being transformed into estimated daily production figures:

Date        VolumeEstimate
2011-08-22  350
2011-08-21  350
2011-08-20  300
2011-08-19  300

Am I on the right track? If so, how can this be implemented? I have absolutely no idea how to do this type of transformation in SQL. If not, what is a better approach?

Note: I cannot do this calculation in application code since that would require a significant code change which we can’t afford.

  • 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-25T00:09:06+00:00Added an answer on May 25, 2026 at 12:09 am

    try

    CREATE TABLE TableA (ProdDate DATE, Volume NUMBER, CollectionNumber NUMBER, ProductionDays NUMBER);
    INSERT INTO TableA VALUES (TO_DATE ('20110822', 'YYYYMMDD'), 500, 1, 2);
    INSERT INTO TableA VALUES (TO_DATE ('20110822', 'YYYYMMDD'), 200, 2, 2);
    INSERT INTO TableA VALUES (TO_DATE ('20110820', 'YYYYMMDD'), 600, 1, 2);
    COMMIT;
    
    CREATE VIEW DailyProdVolEst AS
    SELECT DateList.TheDate, SUM (DateRangeSums.DailySum) VolumeEstimate FROM
    (
    SELECT ProdStart, ProdEnd, SUM (DailyProduction) DailySum
    FROM
    (
    SELECT (ProdDate - ProductionDays + 1) ProdStart, ProdDate ProdEnd, CollectionNumber, VolumeSum/ProductionDays DailyProduction
    FROM
    (
    Select ProdDate, CollectionNumber, ProductionDays, Sum (Volume) VolumeSum FROM TableA
    GROUP BY ProdDate, CollectionNumber, ProductionDays
    )
    )
    GROUP BY ProdStart, ProdEnd
    ) DateRangeSums,
    (
    SELECT A.MinD + MyList.L TheDate FROM
    (SELECT  MIN (ProdDate - ProductionDays + 1) MinD FROM TableA) A,
    (SELECT LEVEL - 1 L FROM DUAL CONNECT BY LEVEL <= (SELECT  Max (ProdDate) - MIN (ProdDate - ProductionDays + 1) + 1 FROM TableA)) MyList
    ) DateList
    WHERE DateList.TheDate BETWEEN DateRangeSums.ProdStart AND DateRangeSums.ProdEnd
    GROUP BY DateList.TheDate;
    

    The view DailyProdVolEst gives you dynamically the result you described… though some “constraints” apply:

    • the combination of ProdDate and CollectionNumber should be unique.
    • the ProductionDays need to be > 0 for all rows

    EDIT – as per comment requested:

    How this query works:
    It finds out what the smallest + biggest date in the table are, then builds rows with each row being a date in that range (DateList)… this is matched up against a list of rows containing the daily sum for unique combinations of ProdDate Start/End (DateRangeSums) and sums it up on the date level.

    What do SUM (DateRangeSums.DailySum) and SUM (DailyProduction) do ?

    Both sum things up – the SUM (DateRangeSums.DailySum) sums up in cases of partialy overlapping date ranges, and the SUM (DailyProduction) sums up within one date range if there are more than one CollectionNumber. Without SUM the GROUP BY wouldn’t be needed.

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

Sidebar

Related Questions

I have data from a table in a database (string) that contain text and
I have a database that contains a table that looks a bit like this:
I have two simple tables in my database. A card table that contains Id,
I have a database structure that has a Person table which contains fields such
We have a data collection system that collects measurements from environmental sensors that measure
Another basic Rails question: I have a database table that needs to contain references
I have a database table that i want to allow my friends to update.
I have a Database table that I want to display in a DataGridView. However,
Suppose I have a database table that has a timedate column of the last
In one of my applications I have a 1gb database table that is used

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.