Solved

Calculating Daily Price

Posted on 2014-12-15
4
105 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
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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
Comment Utility
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 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
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
Comment Utility
Awesome!  Thank You
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now