简体   繁体   中英

Count function in select subquery

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

  • The personnel number stored as foreign key in the hrs_hours table
  • The full name stored in the prs_personel table
  • The sum of all normal hours in seconds from the hrs_hours table
  • The sum of all ort (special) hours in seconds from the hrs_hours table

Part that is not working

  • The count of tasks where hrs_taskname = 'Partus Assistentie' and is performed on the weekend by a personnel member for an entire month in a specific year.
  • The count of tasks where hrs_taskname = 'Partus Assistentie' and is performed on a weekday by a personnel member for an entire month in a specific year.

Results of query:

No rights to place an image, so hope the results are clear in this manner.

  • hrs_prs_fkey (integer): 6 , 11
  • prs_fullname (text): name 1, name 2
  • normal_time (double precision): 46800, 461340
  • ort_time (double precision): 29700, 116100
  • partusass_weekend: 0, 0
  • partusass_week: 2, 2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM