我有一个包含间隔的表格:
CREATE TABLE tbl (
user_id: INTEGER,
start: TIMESTAMP,
end: TIMESTAMP,
billable: BOOLEAN
);
我想创建一个如下所示的视图:
user_id | billable_time | unbillable_time
我可以使用如下查询获取这两个中的一个:
SELECT user_id, sum(end - start) AS billable_time WHERE billable = TRUE GROUP BY user_id;
但是,我需要两个列,无法计时的时间相当于:
SELECT user_id, sum(end - start) AS unbillable_time WHERE billable = FALSE GROUP BY user_id;
如何将这两个值合并到一个查询中?
答案 0 :(得分:3)
select user_id, sum(case billable when 1 then (end - start) end) as can_bill,
sum(case billable when 0 then (end - start) end) as unbillable
答案 1 :(得分:0)
SELECT user_id, billable, sum(end - start) AS billable_time
GROUP BY user_id,billable;