Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to aggregate boolean column

Tags:

sql

oracle

How can I aggregate some tuples like this

COL_1 | COL_2 | COL_3 | COL_4  val  |   T   |   F   |   F  val  |   F   |   T   |   F 

with the OR function and return the following table?

COL_1 | COL_2 | COL_3 | COL_4  val  |   T   |   T   |   F 
like image 985
chrisblo Avatar asked Jun 21 '16 14:06

chrisblo


People also ask

How do you aggregate a column in SQL?

use the keyword COUNT to count the number of rows in a column or table. use the keyword AVG to find the mean of a numerical column. use the keyword SUM to find the total of a numerical column when all the values are added together. use the keyword GROUP BY to group by a column in a table.

Which aggregate function is used in data column?

SQL aggregation function is used to perform the calculations on multiple rows of a single column of a table. It returns a single value. It is also used to summarize the data.

Is first () an aggregate function?

FIRST and LAST are very similar functions. Both are aggregate and analytic functions that operate on a set of values from a set of rows that rank as the FIRST or LAST with respect to a given sorting specification. If only one row ranks as FIRST or LAST , the aggregate operates on the set with only one element.

Can you do aggregate in WHERE clause?

An aggregate function can be used in a WHERE clause only if that clause is part of a subquery of a HAVING clause and the column name specified in the expression is a correlated reference to a group.


2 Answers

Simply do a GROUP BY, use MAX() to return T if available, else F.

select col_1, max(col_2), max(col_3), max(col_4) from tablename group by col_1 
like image 182
jarlh Avatar answered Sep 18 '22 08:09

jarlh


Just as a side note (doesn't work with Oracle): In PostgreSQL, you would do this:

SELECT col_1, bool_or(col_2), bool_or(col_3), bool_or(col_4) FROM tablename GROUP BY col_1 ORDER BY col_1 
like image 32
Cito Avatar answered Sep 22 '22 08:09

Cito