0

我正在尝试按名称对结果进行分组。

以下代码重现:

    name           |    time_     |   
-----------------------------------
    stackoverflow       3:00
    google              5:35
    stackoverflow       1:00

SELECT agenc.name, (SELECT CAST(servic.end_hour as time) - CAST(servic.begin_hour as time)) AS time_, jobs.name,
FROM services AS servic  
JOIN services_jobs AS jobs ON jobs.id = servic.job_id  
JOIN agency AS agenc ON agenc.id = jobs.agency_id
WHERE  
EXTRACT(MONTH FROM servic.service_date) = 9 AND  
EXTRACT(DAY FROM servic.service_date) = 16

我希望它以这种方式出现:

    name           |    time_     |   
-----------------------------------
    stackoverflow       4:00
    google              5:35

可能吗?如果是这样,怎么做?

我想过分组,但我没有得到任何结果。

4

1 回答 1

1

在这里很难提供帮助,我猜您正在寻找sumgroup by

select 
  agenc.name, 
  sum(cast(servic.end_hour as time) - cast(servic.begin_hour as time)) as time_
from 
  services as servic  
  join services_jobs AS jobs ON jobs.id = servic.job_id  
  join agency as agenc ON agenc.id = jobs.agency_id
where  
  extract(month from servic.service_date) = 9 and  
  extract(day from servic.service_date) = 16
group by
  agenc.name
于 2013-09-17T09:53:44.780 回答