A query would be very simple if a table had been properly normalized.
As mentioned in the comments, there are two issues:
next_to_check
and next_to_check2
are two columns that store the same values
- both columns contains lists of values separated by commas, instead of individual values
The table should look like this:
SELECT * From Table where id= 24
Id | next_to_check |
24| 34 |
24| 23 |
24| 4 |
where a type of next_to_check
column must be the same as id
column to avoid unnecessary casting.
For the above table the query may be just:
SELECT *
FROM "TABLE"
start with id = 24
connect by id = prior next_to_check;
If the table cannot be normalized, then you can normalize data "on the fly" using a query like this:
WITH normalized_data As (
SELECT id, trim(regexp_substr(next_to_check, '[^,]+', 1, LEVEL)) next_to_check
FROM "TABLE"
CONNECT BY LEVEL <= regexp_count(next_to_check, ',')+1
UNION ALL
SELECT id, trim(regexp_substr(next_to_check_2, '[^,]+', 1, LEVEL)) next_to_check
FROM "TABLE"
CONNECT BY LEVEL <= regexp_count(next_to_check_2, ',')+1
)
SELECT * FROM normalized_data
and then glue the first query to the above query:
WITH normalized_data As (
SELECT id, trim(regexp_substr(next_to_check, '[^,]+', 1, LEVEL)) next_to_check
FROM "TABLE"
CONNECT BY LEVEL <= regexp_count(next_to_check, ',')+1
UNION ALL
SELECT id, trim(regexp_substr(next_to_check_2, '[^,]+', 1, LEVEL)) next_to_check
FROM "TABLE"
CONNECT BY LEVEL <= regexp_count(next_to_check_2, ',')+1
)
SELECT * FROM normalized_data
start with id = 24
connect by id = prior next_to_check;
but a performance of this "workaround" will be poor, it may work for 100 or 1000 records, but it take years on a bigger table.