这是基于前一个问题的子问题:
Split values from an interval and group by isoweek - Postgresql
问题是如何通过isoweek
对此进行分组CREATE TABLE task
(id int4, start date, stop date, hr int4);
INSERT INTO task
(id, start, stop, hr)
VALUES
(1, '2017-01-01','2017-01-31', 80),
(2, '2017-01-01','2017-02-28', 120);
根据Patricks的回答,我找到了这个解决方案:
SELECT id,to_char(iso, 'iyyy-iw'),(hr/weeks)::numeric (5,2) as hr_week
FROM (SELECT id,hr,generate_series(start,stop,interval '1 week') as iso,
(stop - start)/7 as weeks FROM task) as sub
http://sqlfiddle.com/#!15/93ee1/78
下一步是“分组”,如下所示:
2016-52 35
2017-01 35
2017-02 35
2017-03 35
2017-04 35
2017-05 15
2017-06 15
2017-07 15
2017-08 15
我无法弄清楚如何做到这一点。任何帮助表示赞赏。
TIA,
答案 0 :(得分:1)
PostgreSQL 9.3架构设置:
CREATE TABLE task
(id int4, start date, stop date, hr int4);
INSERT INTO task
(id, start, stop, hr)
VALUES
(1, '2017-01-01','2017-01-31', 80),
(2, '2017-01-01','2017-02-28', 120);
查询1 :
SELECT
to_char(iso, 'iyyy-iw') as YYY_WK
, max(weeks) as weeks
, sum((hr/weeks)::numeric (5,2)) as hr_week
FROM (
SELECT
id
, hr
, generate_series(start,stop,interval '1 week') as iso
, (stop - start)/7 as weeks
FROM task
) as sub
group by
to_char(iso, 'iyyy-iw')
<强> Results 强>:
| yyy_wk | weeks | hr_week |
|---------|-------|---------|
| 2017-08 | 8 | 15 |
| 2017-06 | 8 | 15 |
| 2017-02 | 8 | 35 |
| 2017-03 | 8 | 35 |
| 2017-07 | 8 | 15 |
| 2016-52 | 8 | 35 |
| 2017-05 | 8 | 15 |
| 2017-01 | 8 | 35 |
| 2017-04 | 8 | 35 |
答案 1 :(得分:0)
同时我找到了另一种基于cte的解决方案。然而,这不包括空周,但也是一个解决方案。我会认为@ used-by-already是一个更可靠的答案。
UrlLibrary sportsurl= new UrlLibrary(sports);