Solved

Calculating Daily Price

Posted on 2014-12-15
4
113 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 65

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 69

Accepted Solution

by:
Scott Pletcher earned 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

756 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