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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:34:09+00:00 2026-06-06T16:34:09+00:00

Given the following table and data. create table prices (productKey int ,PriceType char(10) ,BeginDate

  • 0

Given the following table and data.

 create table prices
(productKey int
,PriceType char(10)
,BeginDate date
,EndDate date
,price decimal(18,2))

insert into prices(productKey, PriceType,BeginDate,EndDate, price)
values
(1,'LIST','1-1-2010','1-15-2010',10),
(1,'LIST','1-16-2010','10-15-2010',20),
(1,'DISCOUNT','1-10-2010','1-15-2010',-5),
(2,'LIST','2-1-2010','10-15-2010',30),
(2,'LIST','10-16-2010','1-1-9999',35),
(2,'DISCOUNT','2-10-2010','10-25-2010',-10),
(2,'LIST','1-1-2010','1-15-2010',10),
(3,'DISCOUNT','1-12-2010','1-1-9999',-5),
(3,'LIST','1-16-2010','1-1-9999',10)

I need to insert records into that same table that calculates the actual price (list-discount) for each time period.

e.g. for product 1, I should have the following “ACTUAL” records

Begin     End      Price
1-1-2010  1-9-2010 10
1-10-2010  1-15-2010 5
1-16-2010  10-15-2010 20

I kind of have it figured out for anything where a discount starts within a list price span, but I’m at a loss for anything else.

Thanks for the help

EDIT

There can be multiple discounts per ProductKey, but the discount periods won’t overlap. So you could have one for 2010, and another one for 2012, but not 2 for 2010.

Also, if someone can come up with a better title, please do so. My poor brain is completely challenged at this point.

EDIT2

It’s SQL server 2008R2. I’d love a beautiful set based answer (or someone that gives me a start in that direction), but will be just as happy with a cursor solution that works.

  • 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-06-06T16:34:11+00:00Added an answer on June 6, 2026 at 4:34 pm

    Clever puzzle.

    You need to reconstruct all the time spans. To do this, I take out all the dates from the price ranges and reconstruct the possible date ranges.

    with alldates as (select d.*, ROW_NUMBER() over (partition by productkey order by thedate) as seqnum
                      from ((select productkey, BeginDate as thedate from prices)
                            union all
                            (select productkey, enddate as thedate from prices)
                           ) d
                     ),
         datepair as (select d1.productkey, d1.thedate as BeginDate, d2.thedate as EndDate
                      from alldates d1 left outer join 
                           alldates d2
                           on d1.seqnum = d2.seqnum - 1 and d1.productKey = d2.productKey
                     )
    select dp.productkey, dp.BeginDate, dp.EndDate, SUM(p.price)
    from datepair dp join
         prices p
         on dp.productkey = p.productkey and
             dp.BeginDate >= p.BeginDate and
            dp.EndDate <= p.EndDate
    group by dp.productkey, dp.BeginDate, dp.EndDate
    order by 1, 2, 3
    

    I’ve thought about this some more. The basic idea above is correct. The basic idea is to break up the time dimension into intervals where the list and discount is constant over the entire interval. The question is how to create these intervals, which are in the datepairs alias.

    These intervals have just a few rules:

    • A datepair interval can begin when any time period begins.
    • A datepair interval can begin one day after any time period ends.
    • A datepair interval can end when any time period ends
    • A datepair interval can end one day before any time period begins

    Once we have the intervals, it is a simple matter to join in the appropriate list price and discounts for that period. The following query uses this logic:

    with begindates as (select distinct productKey, thedate
                        from ((select productkey, BeginDate as thedate from prices)
                              union all
                              (select productkey, dateadd(d, 1, enddate) as thedate from prices)
                             ) d
                         ),
         enddates as (select distinct productKey, thedate
                      from ((select productkey, DATEADD(d, -1, begindate) as thedate from prices)
                            union all
                            (select productkey, enddate as thedate from prices)
                           ) d
                     ),
         datepair as (select *
                      from (select d1.productkey, d1.thedate as BeginDate,
                                   MIN(d2.thedate) as EndDate
                            from begindates d1 left outer join 
                                 enddates d2
                                 on d1.productKey = d2.productKey and d1.thedate < d2.thedate
                            group by d1.productkey, d1.thedate
                           ) t
                      where BeginDate <> EndDate
                     )
    select dp.productkey, dp.BeginDate, dp.EndDate, SUM(p.price)
    from datepair dp join
         prices p
         on dp.productkey = p.productkey and
             dp.BeginDate >= p.BeginDate and
            dp.EndDate <= p.EndDate
    group by dp.productkey, dp.BeginDate, dp.EndDate
    order by 1, 2, 3  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following table and data CREATE TABLE #temps ( id int, name varchar(max)
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE
Given the following table structure: CREATE TABLE foo ( ID INT NOT NULL AUTO_INCREMENT,
Given the following data table definitions in SQLite Version 3.7.5: CREATE TABLE [Books] (
Given the following very simple table: Create Table tblUserLogins ( LoginNumber int PRIMARY KEY
Given a simple table with the following data: id | result | played ----+--------+------------
Given a table (id, col1, col2), does it make sense to create the following
Is there a proper term in data modeling to describe the following? Given table
I have a organization name table with the following structure given below: CREATE TABLE
Given an Oracle table created using the following: CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME

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.