postgres min / max group来自日期范围的星期几

时间:2016-11-06 05:46:01

标签: postgresql group-by dayofweek

嗨我有这样的表,它有一对一的关系

report_sites

id | date
1  | 2016-11-02 10:00:00
2  | 2016-11-02 11:00:00
3  | 2016-11-02 12:00:00
4  | 2016-11-03 17:00:00
5  | 2016-11-03 16:00:00

report_services

id | min_serving_time | max_serving_time | report_site_id
1  | 00:00:30.094     | 00:15:30.662     | 1
2  | 00:00:07.566     | 00:07:49.72      | 2
3  | 00:00:07.787     | 00:26:27.587     | 3

我想按工作日从日期范围组中选择 MIN(min_serving_time)MAX(max_serving_time)

weekday | min       | max
sunday  | 00:00:05  | 00:08:55
monday  | 00:01:51  | 00:05:21
tuesday |           | 

我可以像这样计算工作日的行数但是如何聚合值

select    
count(extract(dow from report_sites.date) = 1 or null) as monday,
count(extract(dow from report_sites.date) = 2 or null) as tuesday,
count(extract(dow from report_sites.date) = 3 or null) as wednesday,    
count(extract(dow from report_sites.date) = 4 or null) as thursday,    
count(extract(dow from report_sites.date) = 5 or null) as friday   
 from report_sites 
 inner join "report_services" 
 on "report_sites"."id" = "report_services"."report_site_id"
where report_sites.date >='2016-11-02' 
and 
report_sites.date<='2016-11-07'

感谢您的任何帮助和建议。

0 个答案:

没有答案