COUNT_IF (Lakehouse v1)
后缀 _IF 可以附加到任何聚合函数的名称之后。此时,聚合函数会接受一个额外的参数,即条件。
Analyze 语法
Section titled “Analyze 语法”func.count_if(<column>, <cond>)Analyze 示例
Section titled “Analyze 示例”func.count_if(table.status, table.status=='Completed').alias('completed_orders')
| completed_orders ||------------------|| 3 |SQL 示例
Section titled “SQL 示例”COUNT_IF(<column>, <cond>)SQL 示例
Section titled “SQL 示例”创建表并插入示例数据
CREATE TABLE orders ( id INT, customer_id INT, status VARCHAR, total FLOAT);
INSERT INTO orders (id, customer_id, status, total)VALUES (1, 1, 'completed', 100), (2, 2, 'completed', 200), (3, 1, 'pending', 150), (4, 3, 'completed', 250), (5, 2, 'pending', 300);查询演示:统计已完成的订单
SELECT COUNT_IF(status, status = 'completed') AS completed_ordersFROM orders;结果
| completed_orders ||------------------|| 3 |