Solved

Update Previous Price for datatable

Posted on 2013-11-06
6
279 Views
Last Modified: 2013-11-13
I have a table
ModelID,
Code,
BusDate,
Price
PrevPrice


I need to update the PrevPrice by getting the Price value for a given ModelID, Code from the previous BusDate.

The tables primary key is ModelID, Code, BusDate

Example
ModelID, Code, BusDate ,     Price,   PrevPrice
1 ,            'ABX', 10/18/2013 ,  7.00  ,   0
1 ,            'ABX', 10/18/2013 ,  9.00  ,   0
1 ,            'ABX', 10/21/2013 ,  10.00 ,   0
1 ,            'ABX', 10/22/2013 ,  8.00  ,   0

In the above example I would the data after the update statement to be
1 ,            'ABX', 10/18/2013 ,  7.00  ,   0
1 ,            'ABX', 10/18/2013 ,  9.00  ,   7.00
1 ,            'ABX', 10/21/2013 ,  10.00 ,   9.00
1 ,            'ABX', 10/22/2013 ,  8.00  ,   10.00
0
Comment
Question by:countrymeister
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Can we assume a time value is present for the busdate? Otherwise, how would you distinguish
1 ,            'ABX', 10/18/2013 ,  7.00  ,   0
1 ,            'ABX', 10/18/2013 ,  9.00  ,   7.00
from
1 ,            'ABX', 10/18/2013 ,  9.00  ,   0
1 ,            'ABX', 10/18/2013 ,  7.00  ,   9.00
?
0
 
LVL 1

Author Comment

by:countrymeister
Comment Utility
Sorry, my example should have had the following, since ModelID, Code and BusDate are the primary key

ModelID, Code, BusDate ,     Price,   PrevPrice
1 ,            'ABX', 10/17/2013 ,  7.00  ,   0
1 ,            'ABX', 10/18/2013 ,  9.00  ,   0
1 ,            'ABX', 10/21/2013 ,  10.00 ,   0
1 ,            'ABX', 10/22/2013 ,  8.00  ,   0

In the above example I would the data after the update statement to be
1 ,            'ABX', 10/17/2013 ,  7.00  ,   0
1 ,            'ABX', 10/18/2013 ,  9.00  ,   7.00
1 ,            'ABX', 10/21/2013 ,  10.00 ,   9.00
1 ,            'ABX', 10/22/2013 ,  8.00  ,   10.00
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
SQL> select * from yourtable;
   MODELID COD BUSDATE        PRICE  PREVPRICE
---------- --- --------- ---------- ----------
         1 ABX 17-OCT-13          7          0
         1 ABX 18-OCT-13          9          0
         1 ABX 21-OCT-13         10          0
         1 ABX 22-OCT-13          8          0

SQL> merge into yourtable y
  2  using
  3  (select modelid, code, busdate, price,
  4   lag(price) over (partition by modelid, code order by busdate) prevprice
  5   from yourtable) x
  6  on (y.modelid = x.modelid and y.code = x.code and y.busdate = x.busdate)
  7  when matched then
  8  update set y.prevprice = nvl(x.prevprice,0);

4 rows merged.
SQL> select * from yourtable;
   MODELID COD BUSDATE        PRICE  PREVPRICE
---------- --- --------- ---------- ----------
         1 ABX 17-OCT-13          7          0
         1 ABX 18-OCT-13          9          7
         1 ABX 21-OCT-13         10          9
         1 ABX 22-OCT-13          8         10
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:countrymeister
Comment Utility
I cannot use lag as it is a 2012 function. I am using sql 2008
0
 
LVL 31

Accepted Solution

by:
awking00 earned 400 total points
Comment Utility
Try this -
merge into yourtable a
using
(select x.modelid, x.code, x.busdate, x.price, nvl(y.price,0) as prevprice from
 (select modelid, code, busdate, price,
  row_number() over (partition by modelid, code order by busdate) rn
  from yourtable) x left join
 (select modelid, code, busdate, price,
  row_number() over (partition by modelid, code order by busdate) rn
  from yourtable) y
 on y.rn = x.rn - 1) z
on (a.modelid = z.modelid and a.code = z.code and a.busdate = z.busdate)
when matched then
update set prevprice = z.prevprice;
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 100 total points
Comment Utility
You can try like this.
;with cte as (
  select *,row_number() over (partition by ModelID,Code order by BusDate) rn
    from test)
update c1
   set c1.PrevPrice = coalesce(c2.Price,0)
  from cte c1
  left join cte c2 on c1.rn = c2.rn + 1

Open in new window


check this. http://sqlfiddle.com/#!3/785590/13
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

762 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

10 Experts available now in Live!

Get 1:1 Help Now