Skip to Content
author's profile photo Former Member
Former Member

How to do compare cross rows and cross fields using SQL Script

Hi Experts,

I am trying to create a SQL Script for below requirement:

Recognize the invalid record in raw table and only save the valid records into new table

The raw table is like this:

WorkOrder(key)

PlanOrder(key)

PlanOrder_Create Date

Plan Finish Date

W1

W1P1

D1

D10

W1

W1P2

D3

D5

W1

W1P3

D4

D7

W1

W1P4

D8

D9

The logic is:

For each row(row a) in above table, if there is another row(row b) match below condition:

  1. a.WorkOrder = b.WorkOrder -àthis means same workorder

  1. a. "PlanOrder_Create Date"< b. "PlanOrder_Create Date" -àthis means b is newer

  1. a."Plan Finish Date" > b. "PlanOrder_Create Date" -àthis means when I use "Plan Finish Date" as date Key to show the report, before a."Plan Finish Date" can be used in the report, it already been replaced by b."Plan Finish Date"

If match above 3 conditions, record a will be deleted, so after run this logic, the new table should be like below, only 2 records left:

WorkOrder

PlanOrder

PlanOrder_Create Date

Plan Finish Date

W1

W1P1

D1

D10

W1

W1P4

D8

D9

Currently I am using Cursor go through every record to compare, while the performance is like a dog, below is my code:

DECLARE CURSOR cur_raw_log FOR

SELECT

"WorkOrder",

" PlanOrder",

" PlanOrder_Create Date",

" Plan Finish Date"

FROM table123;

FOR cur_row as cur_raw_log DO

SELECT count(*)

INTO v_found

FROM table123

WHERE "WorkOrder " = cur_row."WorkOrder "

AND " PlanOrder_Create Date " > cur_row." PlanOrder_Create Date "

AND " PlanOrder_Create Date" < cur_row." Plan Finish Date"

;

IF :v_found = 0

THEN put into another table;

ELSE not put into another table;

END IF;

END FOR;

So my question is:

Is there another way which can avoid use Cursor and improve the performance.

Thanks for your help!

Dongkai

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Sep 06, 2016 at 07:28 AM

    Hello Dongkai,

    you can do a "cross join" via the WorkOrder information to avoid the imperative cursor logic. But you should consider how many records you have for one WorkOrder id to get not in trouble with memory issues (if you have thousands/millions of records for the same WorkOrder depending on your system size and load this can maybe cause issues).

    select t2."WorkOrder", t2."PlanOrder", t2."PlanOrder_Create Date", t2."Plan Finish Date"
    from table123 as t1
    inner join table123 as t2
    on t1."WorkOrder" = t2."WorkOrder"
    where t2."PlanOrder_Create Date" > t1."PlanOrder_Create Date"
    and t2."PlanOrder_Create Date" < t1."Plan Finish Date";
    
    
    

    Regards,

    Florian

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 06, 2016 at 01:06 PM

    Thanks very much Florian! It work and runs very fast!

    Never think join can be used for same table twice before. It is really a good example! Thanks mate!

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.