?
Solved

Update Previous Price for datatable

Posted on 2013-11-06
6
Medium Priority
?
285 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
[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
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39628104
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
ID: 39628115
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 32

Expert Comment

by:awking00
ID: 39628239
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 1

Author Comment

by:countrymeister
ID: 39628331
I cannot use lag as it is a 2012 function. I am using sql 2008
0
 
LVL 32

Accepted Solution

by:
awking00 earned 1600 total points
ID: 39628447
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 41

Assisted Solution

by:Sharath
Sharath earned 400 total points
ID: 39638028
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

741 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