The expression specified in the HAVING clause in the sub-query Query-1 appears to be the problem. Note that without the expression, the query returns the correct results with 4 rows. Further, looking ...
The wrong query seems to be generated which also returns the wrong results. I'm using 3 tables in my simplified examples (Emp, Dep, EmpDep), to demonstrate the problem (i've attached the full example ...