Postgresql:根据条件更改COUNT值

时间:2015-06-22 07:55:08

标签: postgresql

所以这是我在数据库中的表格:
工人X有这个工作结果BETWEEN '2015-06-01' AND '2015-06-06'
enter image description here

我想要做的是计算工作天数,但我的条件是如果(nb_heures + nb_heures_s) > 4我算了1天,但如果(nb_heures + nb_heures_s) <= 4我算了0.5天。
所以结果我必须从这个表5.5工作日而不是6 我尝试了这个查询,但效果不佳:

SELECT 
    count(CASE WHEN (nb_heures + nb_heures_s) > 4 THEN 1 END) as full_day_work,
    count(CASE WHEN (nb_heures + nb_heures_s) <= 4 THEN 0.5 END) as half_day_work
FROM pointage_full pf
WHERE date_pointage BETWEEN '2015-06-01' AND '2015-06-06'
AND pf.id_salarie = 5

我怎样才能达到我的目标?

1 个答案:

答案 0 :(得分:0)

COUNT(expr)始终返回bigint,因为它只返回expr不为NULL的行数。

您可以改为使用SUM

SELECT SUM(CASE WHEN (nb_heures + nb_heures_s) > 4 THEN 1 ELSE 0.5 END) as number_of_days
FROM pointage_full pf
WHERE date_pointage BETWEEN '2015-06-01' AND '2015-06-06';
相关问题