dbTalk Databases Forums  

count specific data changes in adjacent ordered rows

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss count specific data changes in adjacent ordered rows in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
cate
 
Posts: n/a

Default count specific data changes in adjacent ordered rows - 12-11-2009 , 07:04 PM






a b c time
a b d time
a b e time
a b c time
a b x time
a b d time

I want to count all the c -> x transitions found in adjacent rows.
The table is ordered. The count is one in the above example.
Oracle SQL

Thank you.

Reply With Quote
  #2  
Old   
Charles Hooper
 
Posts: n/a

Default Re: count specific data changes in adjacent ordered rows - 12-11-2009 , 08:43 PM






On Dec 11, 8:04*pm, cate <catebekens... (AT) yahoo (DOT) com> wrote:
Quote:
a b c time
a b d time
a b e time
a b c time
a b x time
a b d time

I want to count all the c -> x transitions found in adjacent rows.
The table is ordered. *The count is one in the above example.
Oracle SQL

Thank you.
I hope that you mean that you are using an ORDER BY clause when
retrieving the rows from the table. Inserting the rows into a table
in a specific order does not mean that Oracle stores the rows in that
particular order, nor does it mean that Oracle will retrieve the rows
in that specific order.

Take a look at the LAG or LEAD analytic functions to look at the
previous or next row, respectively:
http://download.oracle.com/docs/cd/B...s.htm#i1007032

I recently showed someone how to use the LEAD function in this recent
thread:
http://groups.google.com/group/comp....c7420e0e36231d

You can then compare the previous and next values using the DECODE
function:
http://download.oracle.com/docs/cd/B...ctions040..htm

SUM(DECODE(MY_COL,'x',1,0)*DECODE(LAG(MY_COL,1) OVER (ORDER BY
MYCOL2),'c',1,0))

Charles Hooper
Co-author of "Expert Oracle Practices: Oracle Database Administration
from the Oak Table"
http://hoopercharles.wordpress.com/
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.

Reply With Quote
  #3  
Old   
cate
 
Posts: n/a

Default Re: count specific data changes in adjacent ordered rows - 12-12-2009 , 09:09 AM



On Dec 11, 8:43*pm, Charles Hooper <hooperc2... (AT) yahoo (DOT) com> wrote:
Quote:
On Dec 11, 8:04*pm, cate <catebekens... (AT) yahoo (DOT) com> wrote:

a b c time
a b d time
a b e time
a b c time
a b x time
a b d time

I want to count all the c -> x transitions found in adjacent rows.
The table is ordered. *The count is one in the above example.
Oracle SQL

Thank you.

I hope that you mean that you are using an ORDER BY clause when
retrieving the rows from the table. *Inserting the rows into a table
in a specific order does not mean that Oracle stores the rows in that
particular order, nor does it mean that Oracle will retrieve the rows
in that specific order.

Take a look at the LAG or LEAD analytic functions to look at the
previous or next row, respectively:http://download.oracle.com/docs/cd/B.../b14223/analys...

I recently showed someone how to use the LEAD function in this recent
thread:http://groups.google.com/group/comp....rver/browse_th...

You can then compare the previous and next values using the DECODE
function:http://download.oracle.com/docs/cd/B.../b14200/functi...

SUM(DECODE(MY_COL,'x',1,0)*DECODE(LAG(MY_COL,1) OVER (ORDER BY
MYCOL2),'c',1,0))

Charles Hooper
Co-author of "Expert Oracle Practices: Oracle Database Administration
from the Oak Table"http://hoopercharles.wordpress.com/
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.
Worked great (lag - was a new one on me). Thanks
SELECT *
FROM ( SELECT one,
two,
three "this_three",
four,
LAG (three) OVER (ORDER BY four) AS Next_three
FROM tablex
ORDER BY four) lagit
WHERE this_three = 'c' AND next_three = 'x'

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.