Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Monday, April 28, 2014

Oracle Rownum selection using RANK BY AKA resetting ROWNum based on column(s) data change.


How to generate a row number that resets with a given column1 and column2.

So you can do a select:

RANK() OVER (PARTITION BY COLUMN1, COLUMN2 ORDER BY COLUMN1, COLUMN2 desc, COLUMN3)
from blah blah blah
where
get rid of any data you don't want.

So an example in Costpoint would be:
select
empl_id, ORG_ID,
RANK() OVER (PARTITION BY EMPL_ID, ORG_ID ORDER BY EMPL_ID,  EFFECT_DT, ORG_ID) as rownumber,
a.*
from empl_lab_info a;

If you want to see when someone had salary info and history changes group by assigned org with a rownum attached

Tuesday, March 12, 2013

Oracle Regular Expressions for Parsing XML

Oracle Regular Expressions for Parsing XML


Sample SCHED_PARAM values:
1. Schedule Type='Weekly' StartDate='2013-03-11' FirstRunTime='08:00' RunEveryXWeeks='1' DaysOfWeek='32'

2. Schedule Type='MonthlyDayOfWeek' StartDate='2013-03-11' EndDate='2013-03-19' FirstRunTime='08:00' MonthlyNthOccurrence='0' MonthlyDayOfWeek='0' Months='4095'
R

Using the Regular Expression:
Expression 1:
select REGEXP_SUBSTR(REGEXP_SUBSTR(SCHED_PARAM, 'Type=''[[:alnum:]]*''', 1, 1, 'i'), '[^'']+', 1, 2) as Type from SYNCH_SCHED_DETAIL;
Returns:
1. Weekly
2. MonthlyDayOfWeek

Expression 2:
select REGEXP_SUBSTR(REGEXP_SUBSTR(SCHED_PARAM, 'StartDate=''([[:alnum:]]*\-*)*''', 1, 1, 'i'), '[^'']+', 1, 2) from SYNCH_SCHED_DETAIL;
Returns:
1. 2013-03-11
2. 2013-03-11


Expression 3:
select REGEXP_SUBSTR(REGEXP_SUBSTR(SCHED_PARAM, 'FirstRunTime=''([[:alnum:]]*\:*)*''', 1, 1, 'i'), '[^'']+', 1, 2) from SYNCH_SCHED_DETAIL;
Returns:
1. 08:00
2. 08:00


Expression 4:
select To_date(REGEXP_SUBSTR(REGEXP_SUBSTR(SCHED_PARAM, 'StartDate=''([[:alnum:]]*\-*)*''', 1, 1, 'i'), '[^'']+', 1, 2) || ' ' || REGEXP_SUBSTR(REGEXP_SUBSTR(SCHED_PARAM, 'FirstRunTime=''([[:alnum:]]*\:*)*''', 1, 1, 'i'), '[^'']+', 1, 2), 'YYYY-MM-DD HH24:MI') from SYNCH_SCHED_DETAIL;

Will return an oracle date value for the Start Date + First Run Time

I am sure there are better way to code the Reg Ex.
Please leave me comment below if you have a better way