Difference between revisions of "Sql snippets"
Jump to navigation
Jump to search
(Created page with "``` select (select count(distinct status) from T) = 1 ``` will return 1 or 0 (i.e. true or false) depending on whether all of the rows have the same value in Status or not....") |
|||
Line 1: | Line 1: | ||
+ | # See if all values are the same | ||
+ | - https://stackoverflow.com/questions/5449134/check-if-value-in-column-for-all-rows-is-exactly-value | ||
``` | ``` | ||
select (select count(distinct status) from T) = 1 | select (select count(distinct status) from T) = 1 |
Latest revision as of 22:38, 9 September 2023
See if all values are the same
select (select count(distinct status) from T) = 1
will return 1 or 0 (i.e. true or false) depending on whether all of the rows have the same value in Status or not. If you have to deal with NULL values in status:
select exists ( select status from T where status <> 2 or status is null) as StatusContainsOtherThanTwoOrNullValue