I have an query that I am writing where i have a table containing a list of ages, this column can have NULLS. I have been trying to get a result containing users ages 18+ and if the users age is NULL i would like that result as well. Here is a simplified version of what im trying to do below, and the results.
SELECT * FROM TABLE WHERE (Table.Column >= 18 OR Table.Column IS NULL)
-Returns users of all ages and nulls
SELECT * `FROM TABLE WHERE ((Table.Column >= 18) OR (Table.Column IS NULL))
-Returns users of all ages and nulls
SELECT * FROM TABLE WHERE (Table.Column >= 18 NOT BETWEEN 1 AND 17)
-Returns only users 18+, does not return NULLS
SELECT * `FROM TABLE WHERE ((Table.Column >= 18)
-Returns only users 18+, does not return NULLS
Any insight to what may be happening would be a blessing. This is part of a 1300 line query and this is the best i am able to simplify it. I may need you to keep in mind there may be other things going on which im un-able to explain and maybe a hacky work around is in order.
To go into further details, in psuedo code the entire query is as below.
SELECT ColumnA, ColumnB,
CASE
WHEN (Condition 1),
WHEN (Condition 2)
ELSE 'N/A' END AS [Complete],
CASE
WHEN (Condition 1),
WHEN (Condition 2)
ELSE Column END AS [Column],
ColumnC, ColumnD
FROM
LEFT OUTER JOIN Table A on A.Column = B.Column
LEFT OUTER JOIN Table C on A.Column = B.Column
LEFT OUTER JOIN Table D on A.Column = C.Column
WHERE (
Condition1,
and Condition2,
and (Table.Column >= 18 OR Table.Column IS NULL)
)UNION
SELECT
MAX([column]) AS [column],
MAX([MyColumn] AS [My Column],
FROM (
SELECT
column],
MyColumn,
CASE
WHEN (Condition 1),
WHEN (Condition 2)
else 'N/A' end as [Complete]
CASE
WHEN (Condition 1),
WHEN (Condition 2)
ELSE Column END AS [Column],
Column3
WHERE (Condition1)
AND Condition2
)
and (Table.Column >= 18 OR Table.Column IS NULL)
GROUP BY [ColumnName]
UNION
SELECT * FROM TABLE