-4
HAVING 0 = COUNT(1)) AS a

What does this mean ?

SELECT outr.*
  FROM Orders outr
 WHERE
EXISTS (   SELECT *
                    FROM (   SELECT ol.Orders_ID
                               FROM Orderlines AS ol
                              WHERE Status NOT IN ( 5, 6 )
                              GROUP BY ol.Orders_ID
                             HAVING 0 = COUNT(1)) AS a
                   WHERE outr.Orders_ID = a.Orders_ID)
Paul R
  • 208,748
  • 37
  • 389
  • 560
Shoaib Maroof
  • 369
  • 1
  • 3
  • 13

1 Answers1

1

The AS a is an alias for the derived table. Simplified:

SELECT *
FROM (SELECT Orders_ID
      FROM Orderlines
      GROUP BY Order_ID
      HAVING 0 = COUNT(1)
     ) AS a

The HAVING 0 = COUNT(1) means that the query will only return Orders_ID that have a count of 0. Note that COUNT(1) is the same as COUNT(*).

It doesn't make any sense because an Orders_ID will never exist AND have a count of 0.

So, the derived table will never return anything, and since it's in an EXISTS clause to your outer query, neither will the outer query.

Aaron Dietz
  • 10,137
  • 1
  • 14
  • 26