说,我具有这些示例数据的简化表结构:
id | duration_seconds (int) | is_billable (boolean)
1 | 100 | false
2 | 500 | true
3 | 100 | false
4 | 150 | true
是否有可能从单个查询中获得类似结果?
total_duration | billable_duration | non_billable_duration
850 | 650 | 200
您可以使用条件聚合在一个查询中获得不同的总和:
SELECT SUM(duration_seconds ) AS total_duration,
SUM(CASE WHEN is_billable THEN duration_seconds ELSE 0 END) AS billable_duration,
SUM(CASE WHEN NOT is_billable THEN duration_seconds ELSE 0 END) AS non_billable_duration
FROM yourtable
输出(用于样本数据)
total_duration billable_duration non_billable_duration
850 650 200