Skip to content
Advertisement

Returning a number when result set is null

Each lot object contains a corresponding list of work orders. These work orders have tasks assigned to them which are structured by the task set on the lots parent (the phase). I am trying to get the LOT_ID back and a count of TASK_ID where the TASK_ID is found to exist for the where condition.

The problem is if the TASK_ID is not found, the result set is null and the LOT_ID is not returned at all.

I have uploaded a single row for LOT, PHASE, and WORK_ORDER to the following SQLFiddle. I would have added more data but there is a fun limiter .. err I mean character limiter to the editor.

SQLFiddle

The query returns the expected result when the task id is found (46) but no result when the task id is not found (say 41). I’d expect in that case to see something like:

I have a feeling this needs to be wrapped in a sub-query and then joined but I am uncertain what the syntax would be here.

My true objective is to be able to pass a list of TASK_ID and get back any LOT_ID that doesn’t match, but for now I am just doing a query per task until I can figure that out.

Advertisement

Answer

You want to see all lots with their counts for the task. So either outer join the tasks or cross apply their count or use a subquery in the select clause.

or

or

Another option would be to outer join the count and use COALESCE to turn null into zero in your result.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement