如何聚合布尔列

时间:2016-06-21 14:47:16

标签: sql oracle

如何聚合像这样的元组

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

使用OR函数并返回下表?

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

6 个答案:

答案 0 :(得分:16)

只需执行GROUP BY,使用MAX()返回T(如果可用),否则返回F.

select col_1, max(col_2), max(col_3), max(col_4)
from tablename
group by col_1

答案 1 :(得分:8)

正如旁注(不能与Oracle一起使用):在PostgreSQL中,你会这样做:

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

答案 2 :(得分:2)

如果COL_2COL_4是包含charvarcharvarchar2nvarcharnvarchar2的文字列'T''F',然后您可以MAX'T' > 'F',因为'T',即'F'按词汇顺序排在SELECT COL_1, MAX(COL_2) AS COL_2, MAX(COL_3) AS COL_3, MAX(COL_4) AS COL_4 FROM table GROUP BY COL_1 之后。

"T"

说明:如果至少有一个操作数为TRUE,则本地操作OR返回TRUE。如果至少有一个值为"T",则MAX()返回"F",否则为COL_x NUMBER(1) DEFAULT 0 NOT NULL

注意:如果布尔列声明为

MAX(ABS(col_x))

或任何其他数字类型然后我将0,因为负值也计为TRUE。 (如果您有一个附加到布尔值的ComboBox的Access前端,则会生成值-1{ "description": "NativeScript Application", "license": "SEE LICENSE IN <your-license-filename>", "readme": "NativeScript Application", "repository": "<fill-your-repository-here>", "nativescript": { "id": "org.nativescript.testapp", "tns-android": { "version": "2.0.0" } }, "dependencies": { "@angular/common": "2.0.0-rc.1", "@angular/compiler": "2.0.0-rc.1", "@angular/core": "2.0.0-rc.1", "@angular/platform-browser": "2.0.0-rc.1", "@angular/platform-browser-dynamic": "2.0.0-rc.1", "@angular/platform-server": "2.0.0-rc.1", "@angular/router-deprecated": "2.0.0-rc.1", "nativescript-angular": "0.1.1", "nativescript-telerik-ui": "^1.1.1", "tns-core-modules": "^2.0.0" }, "devDependencies": { "babel-traverse": "6.9.0", "babel-types": "6.10.2", "babylon": "6.8.1", "filewalker": "0.1.2", "lazy": "1.0.11", "nativescript-dev-typescript": "^0.3.2", "typescript": "^1.8.10" } } 。)

答案 3 :(得分:0)

SELECT col1 = MAX(CONVERT(tinyint, ISNULL(col1,0))) ...

说明: (S)他首先在col1中获取布尔值,如果为null,则返回false。然后转换为TINYINT数据类型而不是BIT,然后可以使用MAX()聚合函数有效地表示&#34;如果有,则返回true&#34;在你的GROUP BY。

答案 4 :(得分:0)

我建议您尝试一下:

declare @tempTable table(bitval bit)
insert into @tempTable values(1)
insert into @tempTable values(1)
insert into @tempTable values(0)
insert into @tempTable values(1)

select min(cast(bitval as int)) as AndAggregate from @tempTable 
select max(cast(bitval as int)) as ORAggregate from @tempTable 

答案 5 :(得分:0)

对于Amazon Redshift,bool_or函数有效。

WITH b AS (
    SELECT FALSE AS a
    UNION ALL
    SELECT TRUE
    UNION ALL
    SELECT FALSE
)
SELECT bool_or(a)
FROM b;
-- TRUE

WITH b AS (
    SELECT FALSE AS a
    UNION ALL
    SELECT FALSE
    UNION ALL
    SELECT FALSE
)
SELECT bool_or(a)
FROM b;
-- FALSE

https://docs.aws.amazon.com/redshift/latest/dg/r_BOOL_OR.html