3

here is my table records - table name is temp.

1   | java,c,.net
2   | oracle,hadoop,ruby

Actually i am looking for data like this.

1| java
1 | c
1 | .net
2 | oracle
2| hadoop
2 | ruby

I written below query and expected result is not matching. can you please check verify my query why it is causing deplicate,

select id,
regexp_substr(liked,'[^,]+', 1, level) from 
temp connect by regexp_substr(liked,'[^,]+', 1, level) is not null order by id 
1
  • "Result is not matching" is not helpful. Please edit your question and include the results you're getting, along with the results you expected, and the query you used. You've got the last two, but you need to demonstrate in the question what the problem is. Providing an SQLFiddle which demonstrates the problem would be a good idea. Thanks. Commented Apr 17, 2018 at 13:12

2 Answers 2

3

You want to do something like this:

 SELECT id, REGEXP_SUBSTR(liked, '[^,]+', 1, LEVEL)
   FROM temp 
CONNECT BY REGEXP_SUBSTR(liked, '[^,]+', 1, LEVEL) IS NOT NULL
    AND PRIOR id = id
    AND PRIOR SYS_GUID() IS NOT NULL
  ORDER BY id;

This way using DISTINCT is not necessary.

EDIT: Without using a random number in the CONNECT BY clause, you will get an error as Oracle will think it is an infinite loop.

Sign up to request clarification or add additional context in comments.

10 Comments

NOR it is fully safe. Random may (potentially) return the same result two consecutive times.
Not sure what you mean by "fully safe" - care to explain?
Use both columns in the CONNECT BY: AND PRIOR x = x AND PRIOR y = y
Thanks @David Faber.. Working as expected
@DavidFaber, agree with the solution as SYS_GUID() is indeed UNIQUE in this case.
|
0

To get the exact result you're looking for you need to both use DISTINCT and you need to order by LEVEL within ID:

SELECT ID, LIKED
  FROM (select DISTINCT id, level, regexp_substr(liked,'[^,]+', 1, level) as liked
          from temp
          connect by regexp_substr(liked,'[^,]+', 1, level) is not null
          order by id, level);

SQLFiddle here

Best of luck.

1 Comment

This is not the way to go. For already several records, it starts to heavily utilize the DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.