聚合函数正在计算每个父级而不是最近的父级

时间:2014-01-27 08:48:39

标签: sql postgresql

首先,我将展示我的表格结构:

table work
 id  | scid | starttime  | project_id
-----+------+------------+----------------
 293 |    1 | 2013-10-11 |      235
 294 |    2 | 2013-10-15 |      235
 295 |    5 | 2013-10-16 |      236
 296 |    7 | 2013-10-28 |      236


table project
 id  |projecttype_id| description |            name
-----+--------------+-------------+----------------------------
 235 |            1 |             | Project_name1
 236 |            1 |             | Project_name2
 237 |            1 |             | Project_name3
 238 |            1 |             | Project_name4

table task
  id  |  work_id    | taskengine_id | severity | project_id
------+-------------+---------------+----------+----------
 8536 |         294 |             1 | 0        |      235
 8926 |         294 |             1 | 2        |      235
 8458 |         293 |             1 | 3        |      235
 8459 |         293 |             1 | 3        |      235
 8460 |         293 |             1 | 2        |      235
 8461 |         293 |             1 | 2        |      235
 8462 |         293 |             1 | 0        |      235
 8463 |         293 |             1 | 2        |      235
 8464 |         293 |             1 | 0        |      235
 8465 |         293 |             1 | 3        |      235

我想执行查询,将tasks分为严重性类别。但主要标准是仅计算最近一个work中的任务,我的意思是来自work.starttime的最新日期。

我有一个类似的查询:

select p.name as pname,
count(case when t.severity = '0' then 1 else null end) as zero, 
count(case when t.severity = '1' then 1 else null end) as one,  
count(case when t.severity = '2' then 1 else null end) as two,  
count(case when t.severity = '3' then 1 else null end) as three 
from project p, task t 
where p.id = t.project_id 
group p.name 
order p.name;

但是这样我就会得到Projects的{​​{1}}列表,我希望只得到一个与最近工作开始时间相关的列表。任何人都可以给我一些提示,我可以调整我的查询吗?

2 个答案:

答案 0 :(得分:1)

您需要加入max startime,

w.starttime = (SELECT max(starttime) from work w1 where w1.id = t.work_id)

然后

select p.name as pname,
count(case when t.severity = '0' then 1 else null end) as zero, 
count(case when t.severity = '1' then 1 else null end) as one,  
count(case when t.severity = '2' then 1 else null end) as two,  
count(case when t.severity = '3' then 1 else null end) as three 
from project p, task t, work w
where p.id = t.project_id 
and  p.id= w.project_id
and w.starttime = (SELECT max(starttime) from work w1 where w1.id = t.work_id)
group p.name 
order p.name;

答案 1 :(得分:1)

如果我理解正确,你只想从每个项目的最新工作中计算任务。

如果是这样,下面的查询会为你完成这项工作吗?

select p.name as pname,
 count(case when t.severity = '0' then 1 else null end) as zero, 
 count(case when t.severity = '1' then 1 else null end) as one,  
 count(case when t.severity = '2' then 1 else null end) as two,  
 count(case when t.severity = '3' then 1 else null end) as three 
 from project p, task t, work w
   where p.id = t.project_id
   AND w.id = t.work_id
   AND w.id IN (
    SELECT DISTINCT ON (project_id) id
    FROM work
    ORDER BY project_id, starttime DESC
   )
 group BY p.name 
 order BY p.name;

我只添加了部分,您只筛选出每个项目的最新作品。

相关问题