Quantcast
Viewing all articles
Browse latest Browse all 13329

Need to get all children of a parent if present or parent itself

Hi,

I have the below query which in turn goes in the where clause

Code:

SELECT CASE WHEN EXISTS (SELECT child.id FROM InspectionType parent, 
                              InspectionType child WHERE parent.id = child.parentId AND
                              parent.code = 'INSPECTION_TYPE_SAFETY_LIFE' AND parent.zoneId = 10129)
THEN (SELECT child.id FROM InspectionType parent, InspectionType child 
          WHERE parent.id = child.parentId AND parent.code ='INSPECTION_TYPE_SAFETY_LIFE' AND parent.zoneId = 10129) 
ELSE (SELECT id FROM InspectionType WHERE  code = 'INSPECTION_TYPE_SAFETY_LIFE' AND zoneId = 10129) 
END

I am getting below error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I tried to solve this problem with If and else but it is not going well with in-clause

Code:

select * from InspectionType where id in (select IF EXISTS (SELECT child.id FROM InspectionType parent,
                                                InspectionType child WHERE parent.id = child.parentId AND
                                                parent.code = 'INSPECTION_TYPE_SAFETY_LIFE' AND parent.zoneId = 10129)

BEGIN (SELECT child.id FROM InspectionType parent, InspectionType child
                WHERE parent.id = child.parentId AND parent.code ='INSPECTION_TYPE_SAFETY_LIFE' AND parent.zoneId = 10129)
END
ELSE (SELECT id FROM InspectionType WHERE  code = 'INSPECTION_TYPE_SAFETY_LIFE' AND zoneId = 10129) )


I am using select * from InspectionType where id in() for illustation only. The sub-query results will go another query

Code:

SELECT child.id FROM InspectionType parent, InspectionType child
                WHERE parent.id = child.parentId AND parent.code ='INSPECTION_TYPE_SAFETY_LIFE' AND parent.zoneId = 10129

This returns more than one row

I know why is the error message but need to achieve this functionality. Please help with any other ideas

Viewing all articles
Browse latest Browse all 13329

Trending Articles