In an sqlite3 database, I have a column called date. I need a query that, if all date values are identical, returns the identical value. If they are not, it should return NULL.
How can I best express this in SQL?
SELECT CASE WHEN MIN(DateColumn) = MAX(DateColumn) THEN MIN(DateColumn) ELSE NULL -- not needed, added for clarity END AS result FROM TableX ; Not sure about SQLite but in most DBMS, if there is an index on (DateColumn) the MIN/MAX condition will be much more efficient than any query with COUNT().
min and max or to write WHEN COUNT(DISTINCT DateColumn) = 1?