Given:
CASE
WHEN A IS NULL THEN 1 ELSE 0
END AS [A]
, CASE
WHEN B IS NULL THEN 1 ELSE 0
END AS [B]
, CASE
WHEN C IS NULL THEN 1 ELSE 0
END AS [C]
How can I SUM A+B+C being the results of 3 different CASE WHEN
casesql serversum
Given:
CASE
WHEN A IS NULL THEN 1 ELSE 0
END AS [A]
, CASE
WHEN B IS NULL THEN 1 ELSE 0
END AS [B]
, CASE
WHEN C IS NULL THEN 1 ELSE 0
END AS [C]
How can I SUM A+B+C being the results of 3 different CASE WHEN
Best Answer
You did not specify your RDBMS (SQL Server, Oracle, etc.), but if it supports Common Table Expressions, you could encapsulate your main select logic in a
CTE
and select/sum from it. Here is a solution using SQL Server.As a comment pointed out, you don't need a common table expression. A derived table will also serve the purpose