2
与像一个PostgreSQL 9.6表结构:分钟之间PostgreSQL的最大值在
who | when
每天有相同谁和不同当多个记录。 专注于每个记录是在或出动作为谁要,所以需要为每个谁获得总时间。
即
who | when
A | 2017-03-01 08:00
A | 2017-03-01 12:00
A | 2017-03-01 13:00
A | 2017-03-01 15:00
我怎样才能在总的小时?
我认为max(when) - min(when)得到周期,但需要减去计算中间最小值和最大值的中间数据。
所以需要得到作为12:00“morningout”和13:00为“afternoonin”但是当我把betweeen最小最大在它抱怨
“没有聚合函数可能在”
select who,
to_char(date_trunc('day', when), 'YYYY-MM-DD') "thisday",
count(who) as 'signIn'
min(when) as 'morningout'
max(when) as 'afternoonin'
from the_table
where when between max(when) and min(when)
group by who, "thisday"
order by who;
通常情况下,您将WHERE放在FROM和GROUP BY之间。对于聚合函数条件,您需要一个HAVING子句。 – jarlh
什么时候在the_table的最大值(when)和min(when)之间? –