with a Postgresql 9.6 table structure like:
who | when
Each day has multiple records with same who and different when. Ipotetically each record is a in or out action for who, so need to get total time in for each who.
i.e.
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
how can I get the total of 6hours in?
I think that max(when) - min(when) gets the period but need to subtract the middle data calculating middle min and max.
So need to get the 12:00 as 'morningout' and 13:00 as 'afternoonin' but when I put the betweeen min max in where it complains
'no aggregate function possible in where'
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;