SQL Server: case case

I have a table named boolean

that contains the values ​​"true" and / or "false" as rows in one column.

I am having a problem creating a statement case

to show me if only values ​​are "true" or "false" or "both"

Example 1:

'true'  
'true'  

      

result: 'true'

Example 2:

'false'   
'false'  
'false' 

      

Result: "false"

Example 3:

'true'  
'false'  
'true' 

      

Result: "both"

Edit:

case

the expression should look like this:

case 
   when  "column content are only true values"  then 'true' 
   when  "column content are only false values" then 'false'
   else 'both' 
end

      

+3


source to share


3 answers


You can combine columns in max

and min

in a column and then evaluate the results - if they are the same, there will be only one value in the column. If not, then both should be. Note that since these are string representations, the values ​​are sorted lexicographically:



SELECT CASE WHEN MAX(col) = MIN(col) THEN MAX(col) ELSE 'both' END
FROM   my_table

      

+8


source


SELECT CASE WHEN MIN(Col) <> MAX(Col) THEN 
          'Both'
       ELSE
          MIN(Col)
       END
FROM YourTable

      



+4


source


select case when count(distinct bool_column) = 2 then 'both'
            when sum(case when bool_column = 'false' then 1 end) > 0 then 'false'
            else 'true'
       end as result
from your_table

      

+2


source







All Articles