0

Oracle query is:

select  t3.id, 
        t4.caption || '>' || t3.caption  
from (
    SELECT  t1.id as id, 
            (t2.caption || '>' || t1.caption) as caption, 
            t2.pid as pid  
    FROM ParentChild T1, ParentChild T2 
    WHERE T1.pid = T2.id(+)
) t3, 
(
    SELECT  t1.id as id, 
            (t2.caption || '>' || t1.caption) as caption, 
            t2.pid as pid  
    FROM ParentChild T1, ParentChild T2 
    WHERE T1.pid = T2.id(+)
) t4 
where t3.pid = t4.id(+);

Output:

ID                     T4.CAPTION||'>'||T3.CAPTION
---------------------- ---------------------------
4                      a>b>c>d                    
3                      >a>b>c                     
1                      >>a                        
2                      >a>b                       
5                      >a>e

I need the equivalent SQL Server query...

2
  • You Probably need to learn How to ask a question Commented Jun 30, 2016 at 9:33
  • Well, Oracle old style left join should be replaced with proper explicit left join, and oracle || string concatenation should be replaced with +. Commented Jun 30, 2016 at 9:35

1 Answer 1

2

Try following query;)

SELECT t3.id, t4.caption + '>' + t3.caption
FROM (
    SELECT t1.id as id, (t2.caption + '>' + t1.caption) as caption, t2.pid as pid 
    FROM ParentChild T1
    LEFT JOIN ParentChild T2
    ON T1.pid = T2.id) t3
LEFT JOIN (
    SELECT t1.id as id, (t2.caption + '>' + t1.caption) as caption, t2.pid as pid  
    FROM ParentChild T1
    LEFT JOIN ParentChild T2
    ON T1.pid = T2.id) t4 ON t3.pid = t4.id;
Sign up to request clarification or add additional context in comments.

2 Comments

id (No column name) 1 NULL 2 NULL 3 NULL 4 a>b>c>d 5 NULL Am run this above query shown output like this
Is there the same data between two databases?

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.