0

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?

2 Answers 2

4
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().

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the quick answer. Is it better to use min and max or to write WHEN COUNT(DISTINCT DateColumn) = 1?
1
SELECT CASE WHEN COUNT(DISTINCT DateColumn) = 1 THEN DateColumn ELSE NULL END AS result FROM YourTable; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.