简体   繁体   English

选择子查询中的计数功能

[英]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 作为外键存储在hrs_hours表中的人员编号
  • The full name stored in the prs_personel table 全名存储在prs_personel表中
  • The sum of all normal hours in seconds from the hrs_hours table hrs_hours表中所有正常小时的总和,以秒为单位
  • The sum of all ort (special) hours in seconds from the hrs_hours table hrs_hours表中所有秒(特殊)小时的总和,以秒为单位

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. hrs_taskname ='Partus Assistentie'的任务计数,该任务计数由工作人员在特定年份的整个月中的周末执行。
  • 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. hrs_taskname ='Partus Assistentie'的任务计数,由工作人员在工作日中于特定年份的整个月中的一个工作日执行。

Results of query: 查询结果:

No rights to place an image, so hope the results are clear in this manner. 没有放置图像的权利,因此希望以这种方式获得清晰的结果。

  • hrs_prs_fkey (integer): 6 , 11 hrs_prs_fkey(整数):6,11
  • prs_fullname (text): name 1, name 2 prs_fullname(文本):名称1,名称2
  • normal_time (double precision): 46800, 461340 normal_time(double precision):46800,461340
  • ort_time (double precision): 29700, 116100 ort_time(双精度):29700、116100
  • partusass_weekend: 0, 0 partusass_weekend:0,0
  • partusass_week: 2, 2 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. 在结果中,您可以看到partusass_week是一年中该任务的总金额。 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? 但是,他们两个都执行了总共2个任务中的1个,因此我希望他们显示1和1而不是2和2。我应该如何表达我的(select count ... etc ...)子查询为了得到我想要的结果?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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