We help IT Professionals succeed at work.

get child records corresponding to parent inclding the record if parent itself is child to someone from the same table

1,477 Views
Last Modified: 2014-05-16
table name file_relation_parent_child

id  parent_file_id child_file_id
1      A            B
2      A            C
3      B            D
4      C            E
5      F            K
6      A            J
7      P            A  

hi,

i need to create a query which will return me the all the child_file_id of the parent_file_id  which is having the refrence including the parent of itself?


if the parent file is refrencing the child id and child is further parent of another file then it is denoted by **
if the parent file is refrencing the child id and child is not further parent of another file then it is denoted by *
if the file searched for is itself child of another file ( A is child of P) then it is denoted by ##

Output
A -> **B
A->  **C
A -> *J
A -> ##P

'
but the last case (if the file searched for is itself child of another file ( A is child of P) then it is denoted by ##) is not coming in the result,how to modify below query

  SELECT    r1.parent_file_id
         || '->'
         || r1.child_file_id
         || CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
    FROM file_relation_parent_child r1
         LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
   WHERE r1.parent_file_id = 'A'
ORDER BY r1.parent_file_id, r1.child_file_id
Comment
Watch Question

Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
oracle

SELECT    r1.parent_file_id
       || '->'
       || CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
       || r1.child_file_id
  FROM file_relation_parent_child r1
       LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE r1.parent_file_id = 'A'
UNION ALL
SELECT r1.child_file_id || '->##' || r1.parent_file_id
  FROM file_relation_parent_child r1
       JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE r1.child_file_id = 'A' AND ROWNUM = 1


sql server

SELECT    r1.parent_file_id
       + '->'
       + CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
       + r1.child_file_id
  FROM file_relation_parent_child r1
       LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE r1.parent_file_id = 'A'
UNION ALL
SELECT TOP 1r1.child_file_id + '->##' + r1.parent_file_id
  FROM file_relation_parent_child r1
       JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE r1.child_file_id = 'A'
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
another option

oracle

SELECT DISTINCT
          r1.parent_file_id
       || '->'
       || CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
       || r1.child_file_id
  FROM file_relation_parent_child r1
       LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE 'A' IN (r1.parent_file_id, r1.child_file_id);


sql server


SELECT DISTINCT
          r1.parent_file_id
       + '->'
       + CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
       + r1.child_file_id
  FROM file_relation_parent_child r1
       LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE 'A' IN (r1.parent_file_id, r1.child_file_id);

Author

Commented:
why join is used in second query instead of outer join?
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
oops good catch.  You don't even need the join in the second query
that was a bad cut-n-paste without cleanup on my part.  


SELECT    r1.parent_file_id
       || '->'
       || CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
       || r1.child_file_id
  FROM file_relation_parent_child r1
       LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE r1.parent_file_id = 'A'
UNION ALL
SELECT r1.child_file_id || '->##' || r1.parent_file_id
  FROM file_relation_parent_child r1      
 WHERE r1.child_file_id = 'A' AND ROWNUM = 1


SELECT    r1.parent_file_id
       + '->'
       + CASE WHEN r2.id IS NOT NULL THEN '**' ELSE '*' END
       + r1.child_file_id
  FROM file_relation_parent_child r1
       LEFT OUTER JOIN file_relation_parent_child r2 ON r1.child_file_id = r2.parent_file_id
 WHERE r1.parent_file_id = 'A'
UNION ALL
SELECT TOP 1r1.child_file_id + '->##' + r1.parent_file_id
  FROM file_relation_parent_child r1      
 WHERE r1.child_file_id = 'A'
Database Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
i think union also returning the same result why used union all ?
I there any performance hit if we use union and union all ?
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
UNION forces sorting and comparing both sets to ensure the final results are distinct which is completely unnecessary since the two portions are guaranteed to be distinct.

UNION ALL skips those steps


For small data samples you won't see a difference but UNION ALL is more correct for this type of query

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.