Solved

display results from previos rows

Posted on 2016-08-08
3
30 Views
Last Modified: 2016-09-26
I had posted a question last week and I did accept a solution but looks like I cant completely achieve what I need, may be because I could not explain the logic properly. So here's one more try.
Columns A-H are the columns in existing Oracle view, I need to add an additional column, column I. Column J is what I am getting from my current script.

The business logic is:
if an employee is rehired within 12 months of termination then its term_Service_date (new field) should be its original hire_effect_date. but if he is rehired after 12 months then his term_Service_date (new field) should be his new hire_effect_date.

using my current script, i can get the correct term_Service_dates on the rows where it satisfies the logic but i want to repeat the same hire_effect_date until it finds the next hire_effect_date and if that hire_effect_date is > term_date + 12 months.

select
    employee, hire_date, term_flag,
    term_date, term_code, hire_flag, hire_effect_date, hire_code,
    case hire_flag
      when 1 then
            case when (add_months(term_date, 12) < hire_effect_date)
                        then hire_effect_date
                        else hire_date
                        end
      else
            hire_effect_date
      end   term_SERVICE_DATE  
from
    table1
where
    employee = 1234
order by effect_date desc
test-data.xlsx
0
Comment
Question by:need_solution
3 Comments
 
LVL 15

Accepted Solution

by:
Franck Pachot earned 250 total points
ID: 41748318
Hi, Sorry to not build a full query here, bet basically there are two ways to compare a row with values from other row:
 - analytic functions (aka windowing functions) such as LAG()
 -self join with same table
Regards,
Franck.
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 41749397
What is your oracle version. wanted to know if your version supports LEAD, LAG? Also, do you have any other field which is same for the rows from TERM to REHIRE?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now