Following query correctly displays all current/prospective customers' record regardless of whether an order was placed by him/her or not. But the YesNO
alias of the SELECT
statement below is always returning 1 even when in some cases o.OrderID
is null (i.e., when a perspective customer has not placed an order yet). Why? I think the CASE statement below is correct (as shown in this response as well). I'm using SQL Server 2012
. NOTE: Please keep in mind that although the OrderID is a PK it will always be null along with all other columns of orders table in an OUTER JOIN if the join condition is not met.
SELECT c.customerID, o.OrderID, CASE When o.OrderID is NULL Then 0 Else 1 End as YesNO
FROM Customers c
LEFT JOIN Orders o
ON c.customerID = o.customerID