BOOL_OR [analytic]
返回
窗口内表达式的布尔值。如果至少一个输入值为 true,则 BOOL_OR
返回 t
。否则,它返回 f
。
行为类型
不可变语法
BOOL_OR ( expression ) OVER (
[ window-partition-clause ]
[ window-order-clause ]
[ window-frame-clause ] )
参数
示例
以下示例说明了如何使用 BOOL_AND
、BOOL_OR
和 BOOL_XOR
分析函数。示例表 employee 包含一个用于表示员工类型和年薪的列。
=> CREATE TABLE employee(emptype VARCHAR, yearspaid VARCHAR);
CREATE TABLE
将样本数据插入表中以显示年薪。在多种情况下,员工可以在一年内多次获得报酬。
=> INSERT INTO employee
SELECT 'contractor1', '2014'
UNION ALL
SELECT 'contractor2', '2015'
UNION ALL
SELECT 'contractor3', '2014'
UNION ALL
SELECT 'contractor1', '2014'
UNION ALL
SELECT 'contractor2', '2014'
UNION ALL
SELECT 'contractor3', '2015'
UNION ALL
SELECT 'contractor4', '2014'
UNION ALL
SELECT 'contractor4', '2014'
UNION ALL
SELECT 'contractor5', '2015'
UNION ALL
SELECT 'contractor5', '2016';
OUTPUT
--------
10
(1 row)
查询该表。结果显示在 2014 年获得了两次报酬的员工 (BOOL_AND
),在 2014 年获得了一次或两次报酬的员工 (BOOL_OR
),以及专门显示在 2014 年获得不超过一次报酬的员工 (BOOL_XOR
)。
=> SELECT DISTINCT emptype,
BOOL_AND(yearspaid='2014') OVER (PARTITION BY emptype) AS paidtwicein2014,
BOOL_OR(yearspaid='2014') OVER (PARTITION BY emptype) AS paidonceortwicein2014,
BOOL_XOR(yearspaid='2014') OVER (PARTITION BY emptype) AS paidjustoncein2014
FROM employee;
emptype | paidtwicein2014 | paidonceortwicein2014 | paidjustoncein2014
-------------+-----------------+-----------------------+--------------------
contractor1 | t | t | f
contractor2 | f | t | t
contractor3 | f | t | t
contractor4 | t | t | f
contractor5 | f | f | f
(5 rows)