Oracle SQL Find Sequential Values Based on Criteria in Another Column

mlin

I am doing a project to find a list of paired left right parts in a table. The table contains a column of Unique Part IDs and a column of Left or Right attributes.

If there is a right left pair in the table then the left ID will appear one Unique Part ID before the Right (ID 4 will be Left and ID 5 will be Right). However, there are many Unique IDs between each Left Right Pair that do not have a pair. I'm trying to write a query to find all Left and Right pairs within the table.

For example (currently Both ID and LR are VARCHAR2)

ID  LR
1   L
2   R
5   R
9   R
22  R
34  L
35  R
38  L
91  L
92  R

and I need the query to return

ID  LR
1   L
2   R
34  L
35  R
91  L
92  R

My current line of thinking is a conditional query to select all IDs and LR when LR is R the ID - 1 exists and that LR is L but I'm not sure...any help would be greatly appreciated! Thank you.

Peter M.

Set operations is one way you could do this.

(edited to rewrite query as per comments - it now handles leading zeros in the varchar ID field)


WITH matches AS (
    SELECT to_number(ID) AS ID_integer
    FROM base_table
    WHERE LR = 'L'
    INTERSECT
    SELECT to_number(ID)-1
    FROM base_table
    WHERE LR = 'R'
)
SELECT bt.ID, bt.LR, m.ID_integer
FROM base_table bt, matches m
WHERE LR = 'L'
AND to_number(bt.ID) = m.ID_integer
UNION
SELECT bt.ID, bt.LR, m.ID_integer
FROM base_table bt, matches m
WHERE LR = 'R'
AND to_number(bt.ID)-1 = m.ID_integer
ORDER BY 3, 2;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server find sum of values based on criteria within another table

Oracle SQL: Obtain count of distinct column values based on another column

Add Values together Based on Criteria in Another Column

Oracle SQL to find incremental bracket based on criteria

Sum values based on distinct values in another column plus another criteria

In BigQuery, shuffle values in one column based on sequential ordering of another column

Find common column values based on another column

Formula to SUM values in mulitple columns, based on criteria from another column

Adding another column based on different criteria (SQL-server)

Calculate Duration based on a criteria in another column in SQL for specific IDs

SQL Query matching same values in same column based on criteria

Updating values in a column based on criteria

Renaming column values based on criteria

Sql server - find ranges of a column based on another

Inserting values based on another column SQL

Filter values based on another column in SQL

How to find value in column based on criteria from one column and nth value in another column in Excel?

How to sum values of a column based on the increment values of another column in SQL

specifying a new column based on the criteria in another column

Oracle Sequential assignment of alphabets based on date column

Find minimum values of df column based on another column

Find standard deviation of a column based of values from another column and group by

Getting all values from one column based on criteria in another column and auto-updating

Dynamically select a column using month and year, then sumif the values based on criteria in another column

Creating a column based on two criteria using max values from another column

In Oracle SQL how can i find all values in one column for which in another column exist more than one distinct value

How to add blocks of sequential numbers to a dataframe based on values in another column in R

How to replace values in columns with its mean based on the sequential information from another column?

SQL select value from one column based on values in another column