There are two tables: user_table
and user_schedule
. Each user has four schedule rows (one for each 6 hour period per day) with one column for every workday.
The desired result is to count the number of users that are present for at least one period per day of the work week. For example, if a user is present two quarters of a day, that counts as one.
This is what I thought would work for a Monday, but it's incorrect:
SELECT
count(CASE WHEN TRUE = ANY(ARRAY_AGG(mon)) THEN 1 ELSE null END) as mon,
FROM user_table
LEFT JOIN schedule_table ON user_table.id = schedule_table.user_id
GROUP BY user_id
I created a SQLFiddle to demonstrate.
EDIT:
I was looking for an summary table like this
+-----+-----+-----+-----+-----+
| Mon | Tue | Wed | Thu | Fri |
+-----+-----+-----+-----+-----+
| 4 | 3 | 4 | 4 | 4 |
+-----+-----+-----+-----+-----+
I am running PostgreSQL 9.3
Here are the table definitions I am using:
CREATE TABLE user_table
(
id integer,
start_dt date
);
CREATE TABLE schedule_table
(
id integer,
user_id integer,
mon boolean,
tue boolean,
wed boolean,
thu boolean,
fri boolean
);
\d tbl
in psql) and your version of Postgres would help.