Solved

Calculating Daily Price

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can we attach PDF to table 2 46
Need more granular date groupings 4 45
What does "Between" mean? 6 48
Error converting data type varchar to numeric. 4 14
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

752 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