Home » SQL & PL/SQL » SQL & PL/SQL » retrieve first changed row
retrieve first changed row [message #648015] Mon, 15 February 2016 00:52 Go to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
we need to fetch the row when comp_rate got changed.
As per attached insert scripts we should get recently changed comp_rate row, i.e 4th row. Please let me know the query.

CREATE TABLE customers
( customer_id number(10) NOT NULL,
  eff_dt varchar2(50) NOT NULL,
  comp_rate number(5) NOT NULL
)


INSERT INTO customers values (1000, '2012-01-01', 100)
INSERT INTO customers values (1000, '2013-01-01', 200)
INSERT INTO customers values (1000, '2014-01-01', 300)
INSERT INTO customers values (1000, '2015-01-01', 400)
INSERT INTO customers values (1000, '2015-03-01', 400)



[mod-edit: contents of text file inserted into message body by bb]

[Updated on: Mon, 15 February 2016 15:43] by Moderator

Report message to a moderator

Re: retrieve first changed row [message #648017 is a reply to message #648015] Mon, 15 February 2016 01:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Define "recently" and "got changed". Why not rows 2 and 3?
Do you want the row as it were before change or as it is after change, or both?

Have a look at LAG/LEAD functions.

Re: retrieve first changed row [message #648018 is a reply to message #648017] Mon, 15 February 2016 01:14 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
we need to fetch recently changed comp_rate row. not able to understand the Lag/Lead functions in sql. Please help me.
Re: retrieve first changed row [message #648019 is a reply to message #648018] Mon, 15 February 2016 01:22 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Hi Michol,

Please consider below table.

customer_id   eff_dt          comp_rate
1000          2012-01-01      100
1000          2013-01-01      200
1000          2014-01-01      300
1000          2015-01-01      400
1000          2015-03-01      400
1000          2015-04-01      400


we should retrieve the recently changed comp_rate field as per the eff_dt field. In the above example the output should be 4th row (i.e 2015-01-01).

Please help me with sql query. Thank You.


[mod-edit: code tags added by bb]

[Updated on: Mon, 15 February 2016 15:45] by Moderator

Report message to a moderator

Re: retrieve first changed row [message #648021 is a reply to message #648019] Mon, 15 February 2016 01:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

1/ You did not specify what "recently" is
2/ Click on the links you will know what LAG and LEAD are.
3/ "Do you want the row as it were before change or as it is after change, or both?"

Please help us to help you answering our questions and trying to do it by yourself with the hints we gave you.

Re: retrieve first changed row [message #648022 is a reply to message #648019] Mon, 15 February 2016 01:40 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
suji6281

not able to understand the Lag/Lead functions in sql


What do you not understand? Did you read the documentation? When are they used? How can you apply them to your problem (in order to get the solution)?
Re: retrieve first changed row [message #648026 is a reply to message #648022] Mon, 15 February 2016 03:07 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Hi Michel,

1/ I am using the word "recently" based on the eff_dt field in the table with respect to today date. eff_dt field is the date field which will stores the date when the comp_rate changed.
2/ looks like LAG/LEAD commands will not work in my case.
3/ I need the row after change.

Thank You. Please help me to get the correct results.


Re: retrieve first changed row [message #648027 is a reply to message #648019] Mon, 15 February 2016 03:14 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
suji6281 wrote on Mon, 15 February 2016 07:22
Hi Michol,

Please consider below table.

customer_id eff_dt comp_rate
1000 2012-01-01 100
1000 2013-01-01 200
1000 2014-01-01 300
1000 2015-01-01 400
1000 2015-03-01 400
1000 2015-04-01 400


we should retrieve the recently changed comp_rate field as per the eff_dt field. In the above example the output should be 4th row (i.e 2015-01-01).

Please help me with sql query. Thank You.



Unless you have a typo there, or I have misread, that is not logical. Why 2015-01-01?

Ed: Seems I might have misread. You want the first date it changed? In that case lag/lead will do it, yes.

[Updated on: Mon, 15 February 2016 03:17]

Report message to a moderator

Re: retrieve first changed row [message #648028 is a reply to message #648027] Mon, 15 February 2016 03:28 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Hi Michel,

Please consider the scenario. I have a table with data as shown. from this table I just want to pick recently changed row. I.e in our example comp_rate field value was changed from 300 to 400 and that was happened in 4 row. Hence I need that row values only. Thank you.
Re: retrieve first changed row [message #648030 is a reply to message #648028] Mon, 15 February 2016 03:56 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
So try using lag/lead (or other similar window functions) to flag which row you need.

Post your (home)work and then we can tell you where it's gone wrong.

[Updated on: Mon, 15 February 2016 04:03]

Report message to a moderator

Re: retrieve first changed row [message #648032 is a reply to message #648030] Mon, 15 February 2016 04:20 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
I was unable to get the desired row using lag/lead functions. Please provide solution.
Re: retrieve first changed row [message #648033 is a reply to message #648032] Mon, 15 February 2016 04:22 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
I was, please provide the attempted solution for guidance.
Re: retrieve first changed row [message #648036 is a reply to message #648026] Mon, 15 February 2016 05:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
1/ I am using the word "recently" based on the eff_dt field in the table with respect to today date. eff_dt field is the date field which will stores the date when the comp_rate changed.


Still don' t know what "recently" means. Is this one day from today? one week? one month? one year? one century?

Quote:
2/ looks like LAG/LEAD commands will not work in my case.


Yes it will

Quote:
3/ I need the row after change.


OK, one point that is now clearly specified.

Re: retrieve first changed row [message #648037 is a reply to message #648036] Mon, 15 February 2016 05:37 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Hi Michel,
let me explain 1st point. As per given data in table, last row was the recent w.r.t today's date. however, there was same value on comp_rate filed with 3 different dates. hence I need to get the row that was previously changed.

Re: retrieve first changed row [message #648040 is a reply to message #648037] Mon, 15 February 2016 06:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So (taken into account your point 3 above) you want the latest change.
And LAG function will give you if a row is a change or not.

[Updated on: Mon, 15 February 2016 06:15]

Report message to a moderator

Re: retrieve first changed row [message #648053 is a reply to message #648040] Mon, 15 February 2016 19:06 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Hi Michel,

can you please help me with the sql query.

ThankYou.
Re: retrieve first changed row [message #648066 is a reply to message #648053] Tue, 16 February 2016 00:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Did you try to apply LAG function?
Here's the first step:
SQL> select * from customers  order by customer_id, eff_dt;
CUSTOMER_ID EFF_DT           COMP_RATE
----------- --------------- ----------
       1000 2012-01-01             100
       1000 2013-01-01             200
       1000 2014-01-01             300
       1000 2015-01-01             400
       1000 2015-03-01             400

5 rows selected.

SQL> select c.*,
  2         lag(comp_rate) over (partition by customer_id order by eff_dt) prev_comp_rate
  3  from customers c
  4  order by customer_id, eff_dt
  5  /
CUSTOMER_ID EFF_DT           COMP_RATE PREV_COMP_RATE
----------- --------------- ---------- --------------
       1000 2012-01-01             100
       1000 2013-01-01             200            100
       1000 2014-01-01             300            200
       1000 2015-01-01             400            300
       1000 2015-03-01             400            400

5 rows selected.

Re: retrieve first changed row [message #648072 is a reply to message #648066] Tue, 16 February 2016 01:07 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Thanks for the query.
But I'm looking for a solution which will fetch only one row i.e 4th row not all five rows. As per your query using LAG function we are deriving all five rows. Kindly help me with SQL query which will return only one row. Thank You.

Re: retrieve first changed row [message #648074 is a reply to message #648072] Tue, 16 February 2016 01:13 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 this is the FIRST step.
You now have to work by yourself and prove you do it posting your tries.
I will surely not provide you the complete query without you not making any effort.

Re: retrieve first changed row [message #648077 is a reply to message #648074] Tue, 16 February 2016 01:28 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
I was trying with this query. but not getting the desired value.
SELECT a.eff_dt
FROM customers a
WHERE EXISTS
(SELECT 'y'
FROM customers b
WHERE a.customer_id = b.customer_id
AND a.comp_rate <> b.comp_rate
AND b.eff_dt <
(SELECT min(c.eff_dt)
FROM customers c
WHERE a.customer_id = c.customer_id
AND a.eff_dt = c.eff_dt
AND a.comp_rate <> c.comp_rate))

Please see this query and help me here.
Re: retrieve first changed row [message #648078 is a reply to message #648077] Tue, 16 February 2016 01:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

1/ FORMAT your post as I did it.
2/ This has nothing to do with what I posted.

Re: retrieve first changed row [message #648092 is a reply to message #648078] Tue, 16 February 2016 05:45 Go to previous messageGo to next message
suji6281
Messages: 134
Registered: September 2014
Senior Member
Here you go with the formatted code as per standards.

SELECT a.eff_dt
FROM customers a
WHERE EXISTS
    (SELECT 'y'
     FROM customers b
     WHERE a.customer_id = b.customer_id
       AND a.comp_rate <> b.comp_rate
       AND b.eff_dt <
         (SELECT min(c.eff_dt)
          FROM customers c
          WHERE a.customer_id = c.customer_id
            AND a.eff_dt = c.eff_dt
            AND a.comp_rate <> c.comp_rate))

Re: retrieve first changed row [message #648142 is a reply to message #648092] Wed, 17 February 2016 12:19 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Take Michel's query and use analytics to get down to one row. Your query will not do it
Re: retrieve first changed row [message #648176 is a reply to message #648142] Thu, 18 February 2016 01:52 Go to previous messageGo to next message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
select * from customers
where rowid IN 
  ( select max(rowid) KEEP (DENSE_RANK last ORDER BY to_date(eff_dt,'YYYY-MM-DD')) from customers );
Re: retrieve first changed row [message #648178 is a reply to message #648176] Thu, 18 February 2016 02:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

How does this give the desired result?

SQL> select * from customers
  2  where rowid IN
  3    ( select max(rowid) KEEP (DENSE_RANK last ORDER BY to_date(eff_dt,'YYYY-MM-DD')) from customers );
CUSTOMER_ID EFF_DT           COMP_RATE
----------- --------------- ----------
       1000 2015-03-01             400

1 row selected.


The result is obviously wrong.
Please test your query before posting it.

Re: retrieve first changed row [message #648180 is a reply to message #648178] Thu, 18 February 2016 02:33 Go to previous messageGo to next message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
My fault, sorry.
select
  max(customer_id) KEEP (DENSE_RANK first ORDER BY eff_dt) customer_id,
  comp_rate,
  min(eff_dt) eff_dt
from customers
group by comp_rate
having count(*)>1
Re: retrieve first changed row [message #648181 is a reply to message #648180] Thu, 18 February 2016 02:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Does not work if you have several customers.
Also does not work if comp_rate is not continuously increasing or decreasing.

Re: retrieve first changed row [message #648183 is a reply to message #648181] Thu, 18 February 2016 03:31 Go to previous messageGo to next message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
Michel Cadot!
Actually we are solving different problems.
Nobody knows what the author does exactly mean.
Not all of the conditions are identified.
We are searching for a spherical horse in a vacuum.
Hi suji6281!
Please consider below table.
c_id   eff_dt       comp_rate
1000, '2012-01-01', 100
1000, '2013-01-01', 200
2000, '2013-02-01', 200
2000, '2013-03-01', 200
1000, '2013-04-01', 200
5000, '2014-01-01', 400
5000, '2014-02-01', 400
5000, '2014-03-01', 400

Please show us the correct output of query in this case.
Re: retrieve first changed row [message #648186 is a reply to message #648183] Thu, 18 February 2016 05:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No the problem is clear, even if not fully specified.
The table customers has 3 columns:
- customer_id, so there can be several customers
- eff_dt, the date of the data
- comp_rate, the data itself

With no more information abut the data, we can say it can have any value in its domain range but what we know is, for each customer, it can change. It can increase, it can decrease, it can stay the same.

The test case is not complete but you can easily add the rows you want.
Your data does not show the case where it decreases.
For me, there is only one row to return for your test case: the second one as it is the lone row where the data change for a customer.

Re: retrieve first changed row [message #648189 is a reply to message #648186] Thu, 18 February 2016 05:55 Go to previous messageGo to next message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
Are you kidding?
And what about
1000, '2013-01-01', 200
1000, '2013-04-01', 200

and
2000, '2013-02-01', 200
2000, '2013-03-01', 200
Re: retrieve first changed row [message #648190 is a reply to message #648189] Thu, 18 February 2016 06:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

There are no change in the data, i.e. comp_rate for the customer.

Re: retrieve first changed row [message #648236 is a reply to message #648190] Fri, 19 February 2016 02:25 Go to previous messageGo to next message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
OK then
My last attempt Smile
SELECT
  customer_id,
  max(eff_dt) eff_dt,
  MAX(comp_rate) KEEP (DENSE_RANK last order by eff_dt) comp_rate
FROM 
(
select
  customer_id,
  comp_rate,
  min(eff_dt) eff_dt
from customers
group by customer_id, comp_rate
)
GROUP BY customer_id
Re: retrieve first changed row [message #648244 is a reply to message #648236] Fri, 19 February 2016 03:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Nope, try again. Smile
Here's a new test case (I changed the type of eff_dt to a real DATE):
drop TABLE customers;
CREATE TABLE customers
( customer_id number(10) NOT NULL,
  eff_dt date NOT NULL,
  comp_rate number(5) NOT NULL
)
/
INSERT INTO customers values (1000, to_date('2012-01-01','YYYY-MM-DD'), 100);
INSERT INTO customers values (1000, to_date('2013-01-01','YYYY-MM-DD'), 200);
INSERT INTO customers values (1000, to_date('2014-01-01','YYYY-MM-DD'), 300);
INSERT INTO customers values (1000, to_date('2015-01-01','YYYY-MM-DD'), 400);
INSERT INTO customers values (1000, to_date('2015-03-01','YYYY-MM-DD'), 400);
INSERT INTO customers values (2000, to_date('2013-06-01','YYYY-MM-DD'), 200);
INSERT INTO customers values (2000, to_date('2013-09-01','YYYY-MM-DD'), 200);
INSERT INTO customers values (1000, to_date('2012-01-01','YYYY-MM-DD'), 100);
INSERT INTO customers values (3000, to_date('2013-01-01','YYYY-MM-DD'), 200);
INSERT INTO customers values (3000, to_date('2014-01-01','YYYY-MM-DD'), 300);
INSERT INTO customers values (3000, to_date('2015-01-01','YYYY-MM-DD'), 200);
INSERT INTO customers values (3000, to_date('2015-03-01','YYYY-MM-DD'), 200);
commit;
select * from customers order by customer_id, eff_dt;

SQL> select * from customers order by customer_id, eff_dt;
CUSTOMER_ID EFF_DT       COMP_RATE
----------- ----------- ----------
       1000 01-JAN-2012        100
       1000 01-JAN-2012        100
       1000 01-JAN-2013        200
       1000 01-JAN-2014        300
       1000 01-JAN-2015        400
       1000 01-MAR-2015        400
       2000 01-JUN-2013        200
       2000 01-SEP-2013        200
       3000 01-JAN-2013        200
       3000 01-JAN-2014        300
       3000 01-JAN-2015        200
       3000 01-MAR-2015        200

The result should be, assuming if there is no change for a customer there is no rows returned:
CUSTOMER_ID EFF_DT       COMP_RATE
----------- ----------- ----------
       1000 01-JAN-2015        400
       3000 01-JAN-2015        200

or, assuming if there is no change for a customer you have to return his first row:
CUSTOMER_ID EFF_DT       COMP_RATE
----------- ----------- ----------
       1000 01-JAN-2015        400
       2000 01-JUN-2013        200
       3000 01-JAN-2015        200


Re: retrieve first changed row [message #648556 is a reply to message #648244] Thu, 25 February 2016 06:57 Go to previous messageGo to next message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
Smile
the second one:
SELECT
  customer_id,
  max(eff_dt) eff_dt,
  max(comp_rate) KEEP (dense_rank last order by eff_dt) comp_rate
FROM 
(
select 
  c.*,
  LAG(comp_rate,1,comp_rate-1) OVER (partition by customer_id ORDER BY eff_dt) lag_comp_rate
from customers c
)
WHERE comp_rate<>lag_comp_rate
GROUP BY customer_id

the first one:
SELECT
  customer_id,
  max(eff_dt) eff_dt,
  max(comp_rate) KEEP (dense_rank last order by eff_dt) comp_rate
FROM 
(
select 
  c.*,
  LAG(comp_rate) OVER (partition by customer_id ORDER BY eff_dt) lag_comp_rate
from customers c
)
WHERE comp_rate<>lag_comp_rate
GROUP BY customer_id

Re: retrieve first changed row [message #648575 is a reply to message #648556] Thu, 25 February 2016 14:13 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Not that efficient. All we need is analytic functions - no need to group by. Compare execution plans:

with t as (
           select  c.*,
                   lag(comp_rate,1,comp_rate)
                     over(
                          partition by customer_id
                          order by eff_dt
                         ) prev_comp_rate,
                   max(comp_rate)
                     over(
                          partition by customer_id
                          order by eff_dt
                          rows between 1 following and unbounded following
                         ) max_rest_of_comp_rate,
                   min(comp_rate)
                     over(
                          partition by customer_id
                          order by eff_dt
                          rows between 1 following and unbounded following
                         ) min_rest_of_comp_rate
             from  customers c
          )
select  customer_id,
        eff_dt,
        comp_rate
  from  t
  where comp_rate != prev_comp_rate
    and comp_rate =  nvl(max_rest_of_comp_rate,comp_rate)
    and comp_rate =  nvl(min_rest_of_comp_rate,comp_rate)
/

CUSTOMER_ID EFF_DT     COMP_RATE
----------- --------- ----------
       1000 01-JAN-15        400
       3000 01-JAN-15        200

SQL> select * from table(dbms_xplan.display_cursor());

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
SQL_ID  4tunsbstpxcdr, child number 0
-------------------------------------
with t as (            select  c.*,
lag(comp_rate,1,comp_rate)                      over(
        partition by customer_id                           order by
eff_dt                          ) prev_comp_rate,
max(comp_rate)                      over(
partition by customer_id                           order by eff_dt
                     rows between 1 following and unbounded following
                       ) max_rest_of_comp_rate,
min(comp_rate)                      over(

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
partition by customer_id                           order by eff_dt
                     rows between 1 following and unbounded following
                       ) min_rest_of_comp_rate              from
customers c           ) select  customer_id,         eff_dt,
comp_rate   from  t   where comp_rate != prev_comp_rate     and
comp_rate =  nvl(max_rest_of_comp_rate,comp_ra

Plan hash value: 3113798517

---------------------------------------------------------------------------------
| Id  | Operation           | Name      | Rows  | Bytes | Cost (%CPU)| Time     |

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |           |       |       |     4 (100)|          |
|*  1 |  VIEW               |           |    12 |   888 |     4  (25)| 00:00:01 |
|   2 |   WINDOW SORT       |           |    12 |   420 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| CUSTOMERS |    12 |   420 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(("COMP_RATE"<>"PREV_COMP_RATE" AND

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
              "COMP_RATE"=NVL("MAX_REST_OF_COMP_RATE","COMP_RATE") AND
              "COMP_RATE"=NVL("MIN_REST_OF_COMP_RATE","COMP_RATE")))

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


40 rows selected.

SELECT
  customer_id,
  max(eff_dt) eff_dt,
  max(comp_rate) KEEP (dense_rank last order by eff_dt) comp_rate
FROM 
(
select 
  c.*,
  LAG(comp_rate) OVER (partition by customer_id ORDER BY eff_dt) lag_comp_rate
from customers c
)
WHERE comp_rate<>lag_comp_rate
GROUP BY customer_id
/

CUSTOMER_ID EFF_DT     COMP_RATE
----------- --------- ----------
       1000 01-JAN-15        400
       3000 01-JAN-15        200

SQL> select * from table(dbms_xplan.display_cursor());

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------
SQL_ID  gnzju1v4szfkh, child number 0
-------------------------------------
SELECT   customer_id,   max(eff_dt) eff_dt,   max(comp_rate) KEEP
(dense_rank last order by eff_dt) comp_rate FROM ( select   c.*,
LAG(comp_rate) OVER (partition by customer_id ORDER BY eff_dt)
lag_comp_rate from customers c ) WHERE comp_rate<>lag_comp_rate GROUP
BY customer_id

Plan hash value: 475449762

----------------------------------------------------------------------------------

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------
| Id  | Operation            | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |           |       |       |     4 (100)|          |
|   1 |  SORT GROUP BY NOSORT|           |    12 |   576 |     4  (25)| 00:00:01 |
|*  2 |   VIEW               |           |    12 |   576 |     4  (25)| 00:00:01 |
|   3 |    WINDOW SORT       |           |    12 |   420 |     4  (25)| 00:00:01 |
|   4 |     TABLE ACCESS FULL| CUSTOMERS |    12 |   420 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------

   2 - filter("COMP_RATE"<>"LAG_COMP_RATE")

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


29 rows selected.


SY.

[Updated on: Thu, 25 February 2016 14:17]

Report message to a moderator

Re: retrieve first changed row [message #648595 is a reply to message #648575] Fri, 26 February 2016 01:48 Go to previous message
varlamovvp
Messages: 13
Registered: February 2016
Location: Russia
Junior Member
Thank you SY!
Obviously your query much better. I have to read more about analytic functions
Previous Topic: composite data type
Next Topic: Setting LIMIT value while using Bulk Collect
Goto Forum:
  


Current Time: Fri Apr 26 10:21:50 CDT 2024