Home » SQL & PL/SQL » SQL & PL/SQL » how to get previous row value?
how to get previous row value? [message #608470] Thu, 20 February 2014 03:44 Go to next message
pmreddy.mahi@gmail.com
Messages: 16
Registered: December 2011
Junior Member
with t as(
SELECT '01-jan-2014' COL1, 'B' COL2, .001 COL3, .0006 COL4 FROM DUAL UNION ALL
SELECT '02-jan-2014' COL1, 'B' COL2, .002 COL3, .0007 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .003 COL3, .0008 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .002 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'C' COL2, .004 COL3, null COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, null COL4 FROM DUAL)
SELECT *
FROM T;

OUTPUT;
01-jan-2014 B 0.001 0.0006
02-jan-2014 B 0.002 0.0007
03-jan-2014 B 0.003 0.0008
03-jan-2014 B 0.002 0.0009
03-jan-2014 C 0.004
04-jan-2014 C 0.005 0.0009
04-jan-2014 C 0.005

Required Output should be:


01-jan-2014 B 0.001 0.0006
02-jan-2014 B 0.002 0.0007
03-jan-2014 B 0.003 0.0007
03-jan-2014 B 0.002 0.0007
03-jan-2014 C 0.004 0.0007
04-jan-2014 C 0.005 0.0009
04-jan-2014 C 0.005 0.0009

If we have multiple dates we need to get previous day value of COL4 and update that value to current day COL4 Rows..
Re: how to get previous row value? [message #608471 is a reply to message #608470] Thu, 20 February 2014 04:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And when there are multiple records for the previous day (as there are in your example) how do we determine which record to take the value from?
Re: how to get previous row value? [message #608473 is a reply to message #608471] Thu, 20 February 2014 04:16 Go to previous messageGo to next message
pmreddy.mahi@gmail.com
Messages: 16
Registered: December 2011
Junior Member
Most recent one
Re: how to get previous row value? [message #608474 is a reply to message #608473] Thu, 20 February 2014 04:18 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And how are we supposed to work that out, seeing as there doesn't appear to be any data to indicaate that?
Or will all records have different times? If so your example should reflect that.
Re: how to get previous row value? [message #608475 is a reply to message #608470] Thu, 20 February 2014 04:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Why
03-jan-2014 B 0.003 0.0007
03-jan-2014 B 0.002 0.0007
03-jan-2014 C 0.004 0.0007
from
03-jan-2014 B 0.003 0.0008
03-jan-2014 B 0.002 0.0009
03-jan-2014 C 0.004
?

Re: how to get previous row value? [message #608476 is a reply to message #608475] Thu, 20 February 2014 04:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And how col2 and col3 come into play in this question?
Are they just fillers (and so should not be in the question)?

Re: how to get previous row value? [message #608477 is a reply to message #608475] Thu, 20 February 2014 04:25 Go to previous messageGo to next message
pmreddy.mahi@gmail.com
Messages: 16
Registered: December 2011
Junior Member
02-jan-2014 B 0.002 0.0007

I want to update 03-jan-2014 values with 02-jan-2014 COL4 value.
Re: how to get previous row value? [message #608480 is a reply to message #608477] Thu, 20 February 2014 04:28 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
you need to answer my question
Re: how to get previous row value? [message #608481 is a reply to message #608477] Thu, 20 February 2014 04:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Why? Requirement is not clear.
Why don't you update 04-jan-2014 with 03-jan-2014 one which is 02-jan-2014 as you say?
Why
04-jan-2014 C 0.005 0.0009
04-jan-2014 C 0.005 0.0009
from
02-jan-2014 B 0.002 0.0007
03-jan-2014 B 0.003 0.0008
03-jan-2014 B 0.002 0.0009
03-jan-2014 C 0.004
04-jan-2014 C 0.005 0.0009
04-jan-2014 C 0.005
?

Also
Quote:
And how col2 and col3 come into play in this question?
Are they just fillers (and so should not be in the question)?


Re: how to get previous row value? [message #608483 is a reply to message #608481] Thu, 20 February 2014 04:31 Go to previous messageGo to next message
pmreddy.mahi@gmail.com
Messages: 16
Registered: December 2011
Junior Member
Sorry!!!

Required Output should be:
01-jan-2014 B 0.001 0.0006
02-jan-2014 B 0.002 0.0007
03-jan-2014 B 0.003 0.0007
03-jan-2014 B 0.002 0.0007
03-jan-2014 C 0.004 0.0007
04-jan-2014 C 0.005 0.0007
04-jan-2014 C 0.005 0.0007

Yes! COL2 and COL4 are just filleres.

[Updated on: Thu, 20 February 2014 04:32]

Report message to a moderator

Re: how to get previous row value? [message #608486 is a reply to message #608474] Thu, 20 February 2014 04:46 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
cookiemonster wrote on Thu, 20 February 2014 10:18
And how are we supposed to work that out, seeing as there doesn't appear to be any data to indicaate that?
Or will all records have different times? If so your example should reflect that.

Re: how to get previous row value? [message #608491 is a reply to message #608486] Thu, 20 February 2014 05:29 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
hi,

Please find the query as per your requirement :


with t as(
SELECT '01-jan-2014' COL1, 'B' COL2, .001 COL3, .0006 COL4 FROM DUAL UNION ALL
SELECT '02-jan-2014' COL1, 'B' COL2, .002 COL3, .0007 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .003 COL3, .0008 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .002 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'C' COL2, .004 COL3, null COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, null COL4 FROM DUAL)
SELECT  col1
       ,col2
       ,col3
       ,case when ROW_NUMBER() OVER ( ORDER BY col1)  >= 2 then         
          ( select   col4 
            from     t 
            where    (col1,col3) in(
                             select  case when ROW_NUMBER() OVER ( ORDER BY col1)  = 2 
                                     then col1 
                                     end,col3
                              from t
                             )
           )  
         else col4
        end as a-----then col4 end end,*/        
FROM T;


[Updated on: Thu, 20 February 2014 05:35]

Report message to a moderator

Re: how to get previous row value? [message #608492 is a reply to message #608491] Thu, 20 February 2014 05:34 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
As I keep pointing out, order by col1 is not deterministic.
So that query is wrong. And no correct query can be given until the OP tells what to use to get a deterministic order.
Re: how to get previous row value? [message #608493 is a reply to message #608492] Thu, 20 February 2014 05:38 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Yes Cookie,

Thanks,I changed col1,col3..... Razz
Re: how to get previous row value? [message #608494 is a reply to message #608493] Thu, 20 February 2014 05:40 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Look at that data for 04-jan, does adding col3 make it deterministic?
Re: how to get previous row value? [message #608495 is a reply to message #608494] Thu, 20 February 2014 05:45 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Yes Cookie,

I need to add col4 also.

Dear OP,

Please find query result :


with t as(
SELECT '01-jan-2014' COL1, 'B' COL2, .001 COL3, .0006 COL4 FROM DUAL UNION ALL
SELECT '02-jan-2014' COL1, 'B' COL2, .002 COL3, .0007 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .003 COL3, .0008 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .002 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'C' COL2, .004 COL3, null COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, null COL4 FROM DUAL)
SELECT  col1
       ,col2
       ,col3
       ,case when ROW_NUMBER() OVER ( ORDER BY col1)  >= 2 then         
          ( select   col4 
            from     t 
            where    (col1,col3,col4) in(
                             select  case when ROW_NUMBER() OVER ( ORDER BY col1)  = 2 
                                     then col1 
                                     end,col3,col4
                              from t
                             )
           )  
         else col4
        end as a-----then col4 end end,*/        
FROM T;

Re: how to get previous row value? [message #608496 is a reply to message #608495] Thu, 20 February 2014 05:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This does not work as in any way OP's said: "COL2 and COL4 are just filleres." (I think he meant COL3 otherwise the whole question is non sense).
So remove them from your query and use ONLY col1 and col4.

Re: how to get previous row value? [message #608498 is a reply to message #608496] Thu, 20 February 2014 06:02 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Thanks Michel sir,

I used only col1 and col4:

Dear OP,

Please find the query result :


with t as(
SELECT '01-jan-2014' COL1, 'B' COL2, .001 COL3, .0006 COL4 FROM DUAL UNION ALL
SELECT '02-jan-2014' COL1, 'B' COL2, .002 COL3, .0007 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .003 COL3, .0008 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'B' COL2, .002 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '03-jan-2014' COL1, 'C' COL2, .004 COL3, null COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, .0009 COL4 FROM DUAL UNION ALL
SELECT '04-jan-2014' COL1, 'C' COL2, .005 COL3, null COL4 FROM DUAL)
SELECT  col1
       ---,col2
      --- ,col3
       ,case when ROW_NUMBER() OVER ( ORDER BY col1)  >= 2 then         
          ( select   col4 
            from     t 
            where    (col1,col4) in(
                             select  case when ROW_NUMBER() OVER ( ORDER BY col1)  = 2 
                                     then col1 
                                     end,
                                    ---- col3,
                                     col4
                              from t
                             )
           )  
         else col4
        end as a-----then col4 end end,*/        
FROM T;



Re: how to get previous row value? [message #608501 is a reply to message #608498] Thu, 20 February 2014 06:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

As I said and cookiemonster repeated, this will not work.
Just add:
UNION ALL
SELECT '02-jan-2014' COL1, 'B' COL2, .002 COL3, .0001 COL4 FROM DUAL
Re: how to get previous row value? [message #608518 is a reply to message #608501] Thu, 20 February 2014 07:36 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Quote:

As I said and cookiemonster repeated, this will not work.


Query is working fine and having no issues with adding 1 extra UNION ALL for '02-jan-2014'
Re: how to get previous row value? [message #608520 is a reply to message #608492] Thu, 20 February 2014 08:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
This is the output your query gives, with just the original rows supplied by the OP:
COL1                 A
----------- ----------
01-jan-2014     0.0006
02-jan-2014     0.0007
03-jan-2014     0.0007
03-jan-2014     0.0007
03-jan-2014     0.0007
04-jan-2014     0.0007
04-jan-2014     0.0007


It does not match the OPs expected output, 4th Jan is wrong.
But before you try and correct this query again I'll repeat myself one last time:

cookiemonster wrote on Thu, 20 February 2014 11:34
As I keep pointing out, order by col1 is not deterministic.
So that query is wrong. And no correct query can be given until the OP tells what to use to get a deterministic order.


You seem determined to ignore that part in bold above. Now unless you're psychic (and if you are I'd like this weeks lottery numbers please) you, like the rest of us, are in position to provide a solution to the OPs problem.
Re: how to get previous row value? [message #608523 is a reply to message #608518] Thu, 20 February 2014 08:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
msol25 wrote on Thu, 20 February 2014 14:36
Quote:

As I said and cookiemonster repeated, this will not work.


Query is working fine and having no issues with adding 1 extra UNION ALL for '02-jan-2014'


Wrong! read again cookiemonster posts.

Re: how to get previous row value? [message #608524 is a reply to message #608523] Thu, 20 February 2014 08:14 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And just to spell out what your current query is doing:
It's replacing col4 of every row after the first (ordered by col1) with the value of col4 from the 2nd row (ordered by col1).
It's not even trying to check which rows are for the previous day at all.
Re: how to get previous row value? [message #608527 is a reply to message #608470] Thu, 20 February 2014 08:24 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Instead of pointing ambiguousness of some code, what about trying to get exact rules for the result set and implement them?
Quote:
If we have multiple dates we need to get previous day value of COL4 and update that value to current day COL4 Rows..

I understand it as: when there are multiple rows for given COL1, fill COL4 with the value of COL4 from the closest previous (single) row (where is only one row with given COL1). Assume sorting by COL1.
with T as (<see first post>)
  -- determine count of rows with the same COL1
  , get_cnts as ( select t.*, count(*) over (partition by col1) nr_rows
                  from T )
  -- take the value from the latest row having that count equal to 1
  , get_rows as ( select get_cnts.*,
                         last_value(case when nr_rows = 1 then col4 end ignore nulls)
                            over ( order by col1 ) prev_nondup_col4
                  from get_cnts )
select * from get_rows;

Of course it would be nice if pmreddy.mahi would be able to clearly state these rules.
Re: how to get previous row value? [message #608529 is a reply to message #608527] Thu, 20 February 2014 08:30 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That doesn't give OPs expected output either. gives 0.0007 for 4th Jan where OP says it should be 0.0009
Re: how to get previous row value? [message #608530 is a reply to message #608529] Thu, 20 February 2014 08:37 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Not, but OP managed to change the required output later: http://www.orafaq.com/forum/mv/msg/191644/608483/#msg_608483

Of course, without knowing the exact rules (and some other examples, e.g. after Michel's modification), it is still only a guess.
Re: how to get previous row value? [message #608531 is a reply to message #608530] Thu, 20 February 2014 08:55 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
True, lost track of that, your query may be correct
Previous Topic: Future Partitions
Next Topic: Timestamp gets truncated from cursor for loop
Goto Forum:
  


Current Time: Thu Apr 25 15:17:01 CDT 2024