?
Solved

Calculating Daily Price

Posted on 2014-12-15
4
Medium Priority
?
117 Views
Last Modified: 2014-12-22
I have a table that holds price change data and I need to be able to calculate what the price was on a given date.  Example What was the price on 2014-08-05.

The ultimate goal is to get the starting and ending price for a date range.  BETWEEN 2014-08-05 AND 2014-08-20

DATE              PRICE  ITEM
2014-08-02      4.46      1
2014-08-09      4.89      1
2014-08-13      4.69      1
2014-08-25      4.99      1
2014-08-30      4.89      1
0
Comment
Question by:bushido121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40501329
Assuming there can be only one row per id / date, give this a whirl..
SELECT id, Price, Max(Date) as current_price_date
FROM YourTable
WHERE Date <= '2014-08-05'
GROUP BY id, Price

SELECT id, Price, Max(Date) as current_price_date
FROM YourTable
WHERE Date <= '2014-08-20'
GROUP BY id, Price

Open in new window

0
 

Author Comment

by:bushido121
ID: 40501489
I don't think I explained it very well.  I'm trying to get the price for that item on 8/5 and the price on 8/20 so I can calculate the difference.

On 8/5 the item was 4.46 (I needed to go back to 8/2 to get the last price change)
On 8/20 the item was 4.69 (I needed to go back to 8/13 to get the last price change)

Ultimately I'm looking for the impact of Item 1 from 8/2 to 8/20.  (4.69 - 4.46 =  .23)

Obviously there are thousands of records in the table.

DATE              PRICE  ITEM
2014-08-02      4.46      1
2014-08-09      4.89      1
2014-08-13      4.69      1
2014-08-25      4.99      1
2014-08-30      4.89      1
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40501611
DROP TABLE #price_data
GO
CREATE TABLE #price_data (
    date date not null,
    price decimal(9, 2) not null,
    item varchar(10) not null
    )
INSERT INTO #price_data SELECT '2014-08-02',      4.46,      1
INSERT INTO #price_data SELECT '2014-08-09' ,    4.89  ,    1
INSERT INTO #price_data SELECT '2014-08-13'  ,  4.69    ,  1
INSERT INTO #price_data SELECT '2014-08-25'  , 4.99  ,    1
INSERT INTO #price_data SELECT '2014-08-30'  ,4.89    ,  1
GO

DECLARE @date1 date
DECLARE @date2 date

SET @date1 = '20140805'
SET @date2 = '20140820'

SELECT pd.item,
    MAX(CASE WHEN pd.date = item_dates.item_date1 THEN price END) AS item_price1,
    MAX(CASE WHEN pd.date = item_dates.item_date2 THEN price END) AS item_price2
    MAX(CASE WHEN pd.date = item_dates.item_date2THEN price END) -
    MAX(CASE WHEN pd.date = item_dates.item_date1THEN price END) AS item_price_diff
FROM (
    SELECT pd2.item,
        MAX(CASE WHEN pd2.date <= @date1 THEN pd2.date END) AS item_date1,
        MAX(CASE WHEN pd2.date <= @date2 THEN pd2.date END) AS item_date2
    FROM #price_data pd2
    WHERE
        pd2.date <= @date2
    GROUP BY pd2.item
) AS item_dates
INNER JOIN #price_data pd ON
    pd.item = item_dates.item AND
    pd.date IN (item_dates.item_date1, item_dates.item_date2)
GROUP BY pd.item
ORDER BY pd.item
0
 

Author Closing Comment

by:bushido121
ID: 40512919
Awesome!  Thank You
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question