Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MAX value of a BIT column

I have a SELECT request with 'inner join' in the joined table is a column with bit type.

I want to select 1 if in the joined table is at most one value with 1. If it is not the case the value will be 0.

So If I have:

PERSID | NAME 1      |  Toto 2      |  Titi 3      |  Tata 

And the second table

PERSID | BOOL 1      |  0 1      |  0 2      |  0 2      |  1 

I would like to have for result

Toto -> 0 Titi -> 1 Tata -> 0 

I try this:

SELECT       sur.*     ,MAX(bo.BOOL)          FROM SURNAME sur                   INNER JOIN BOOL bo     ON bo.IDPERS = sur.IDPERS 

But MAX is not available on BIT column.. So how can I do that?

Thanks,

like image 761
bAN Avatar asked May 29 '12 17:05

bAN


People also ask

How do I find the maximum value of a column in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

Can we use Max on a column?

MAX can be used with numeric, character, and datetime columns, but not with bit columns. Aggregate functions and subqueries are not permitted.

How do I get the maximum value from another column of a table in SQL?

In SQL Server there are several ways to get the MIN or MAX of multiple columns including methods using UNPIVOT, UNION, CASE, etc… However, the simplest method is by using FROM … VALUES i.e. table value constructor. Let's see an example. In this example, there is a table for items with five columns for prices.


2 Answers

Try:

max(cast(bo.BOOL as int)) 
like image 27
Andomar Avatar answered Sep 22 '22 15:09

Andomar


you can cast it to an INT, and even cast it back to a BIT if you need to

SELECT       sur.*     ,CAST(MAX(CAST(bo.BOOL as INT)) AS BIT)     FROM SURNAME sur                   INNER JOIN BOOL bo     ON bo.IDPERS = sur.IDPERS 
like image 152
kenwarner Avatar answered Sep 24 '22 15:09

kenwarner