I'm trying to solve the query below, but can't get it to work completely so really need some help. What I'm trying to get as a result is the following:
Part that is working
Part that is not working
Results of query:
No rights to place an image, so hope the results are clear in this manner.
In the results you can see that the partusass_week is the total amount of those task in the month of a year. However they have both performed 1 of those task totaling the amount 2, and thus I would like them to display 1 and 1 instead of 2 and 2. How should I phrase my (select count ...etc... ) sub-queries in order to get the results that I would like?
SELECT hrs_prs_fkey,
prs_fullname,
SUM(EXTRACT (epoch FROM hrs_normaltime)) AS normal_time,
SUM(EXTRACT (epoch FROM hrs_orttime)) AS ort_time,
(SELECT Count(hrs_taskname) AS partusass_weekend FROM hrs_hours
WHERE hrs_taskname = 'Partus Assistentie'
AND EXTRACT(MONTH FROM "hrs_date") = 7
AND EXTRACT(YEAR FROM "hrs_date") = 2015
AND extract(dow from hrs_date) in (0,6)),
(SELECT Count(hrs_taskname) AS partusass_week FROM hrs_hours
WHERE hrs_taskname = 'Partus Assistentie'
AND EXTRACT(MONTH FROM "hrs_date") = 7
AND EXTRACT(YEAR FROM "hrs_date") = 2015
AND extract(dow from hrs_date) in (1,2,3,4,5))
FROM hrs_hours
LEFT JOIN prs_personel ON hrs_hours.hrs_prs_fkey = prs_personel.prs_pkey
WHERE EXTRACT(MONTH FROM "hrs_date") = 7
AND EXTRACT(YEAR FROM "hrs_date") = 2015
GROUP BY hrs_prs_fkey, prs_fullname
ORDER BY hrs_prs_fkey ASC
Thank you in advance for reading and thinking along.
I think you just want conditional aggregation:
SELECT hrs_prs_fkey, prs_fullname,
SUM(EXTRACT(epoch FROM hrs_normaltime)) AS normal_time,
SUM(EXTRACT(epoch FROM hrs_orttime)) AS ort_time,
SUM(CASE WHEN hrs_taskname = 'Partus Assistentie' AND
extract(dow from hrs_date) in (0, 6)
THEN 1 ELSE 0 END
END),
SUM(CASE WHEN hrs_taskname = 'Partus Assistentie' AND
extract(dow from hrs_date) in (1, 2, 3, 4, 5)
THEN 1 ELSE 0 END
END)
FROM hrs_hours LEFT JOIN
prs_personel
ON hrs_hours.hrs_prs_fkey = prs_personel.prs_pkey
WHERE EXTRACT(MONTH FROM hrs_date) = 7 AND
EXTRACT(YEAR FROM hrs_date) = 2015 AND
GROUP BY hrs_prs_fkey, prs_fullname
ORDER BY hrs_prs_fkey ASC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.