Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calculating Daily Price

Posted on 2014-12-15
4
Medium Priority
?
118 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
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

916 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