postgres Sql查询在generate_series之间的总和

时间:2016-12-20 09:30:57

标签: sql postgresql

抓住我的头。我想在接下来的3天内将所有t1.sales和group汇总成48个周期的生成时间序列。但是,我在下面尝试返回不正确的结果。

SELECT 
seq.date,
hh.period,
sum(t1.sales)
From
(select date(date)from generate_series(current_date,current_date + '3 days'::interval ,'1 day'::interval) date)as seq 
cross join (select period from generate_series (1,48) period) hh
Left join tbl_look t1
ON (
seq.date between t1.from_date and t1.to_date 
and hh.period between t1.from_period and t1.to_period)

tbl_look的示例,请参阅句点33有两行。我需要总和来捕捉'to_period'。在下面的例子中,to_period 33应该总和为13.5,而to_period 34应该是17.8。

from_date     to_date      ;from_period; to_period; sales
"2016-12-19"  "2016-12-19" ;33         ; 48       ; 5.000
"2016-12-19"  "2016-12-19" ;33         ;          ; 8.500
"2016-12-19"  "2016-12-19" ;34         ;          ; 12.800
"2016-12-19"  "2016-12-19" ;35         ;          ; 16.000
"2016-12-19"  "2016-12-19" ;36         ; 38       ; 17.450
"2016-12-19"  "2016-12-19" ;37         ;          ; 17.850
"2016-12-19"  "2016-12-19" ;38         ;          ; 17.400

预期结果

seq.results   hh.period  sales
"2016-12-19"  33         13.5
"2016-12-19"  34         17.8
"2016-12-19"  35         21
"2016-12-19"  36         22.45
"2016-12-19"  37         40.3
"2016-12-19"  38         39.85
"2016-12-19"  39         5
"2016-12-19"  40         5
 ......

2 个答案:

答案 0 :(得分:1)

select      date (dt.date)      as date
           ,pr.period           as period
           ,sum (t.sales)       as sales

from                    generate_series (current_date,current_date+3,'1 day'::interval) as dt (date)

            cross join  generate_series (1,48) as pr (period)

            left join   tbl_look  as t

            on          dt.date   between t.from_date   and t.to_date 
                    and pr.period between t.from_period and coalesce (t.to_period,t.from_period)

group by    dt.date
           ,pr.period

order by  dt.date
         ,pr.period     
+------------+--------+--------+
| date       | period | sales  |
+------------+--------+--------+
| 2016-12-19 | 1      | (null) |
+------------+--------+--------+
| 2016-12-19 | 2      | (null) |
+------------+--------+--------+
.
.
.
+------------+--------+--------+
| 2016-12-19 | 32     | (null) |
+------------+--------+--------+
| 2016-12-19 | 33     | 13.500 |
+------------+--------+--------+
| 2016-12-19 | 34     | 17.800 |
+------------+--------+--------+
| 2016-12-19 | 35     | 21.000 |
+------------+--------+--------+
| 2016-12-19 | 36     | 22.450 |
+------------+--------+--------+
| 2016-12-19 | 37     | 40.300 |
+------------+--------+--------+
| 2016-12-19 | 38     | 39.850 |
+------------+--------+--------+
| 2016-12-19 | 39     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 40     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 41     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 42     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 43     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 44     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 45     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 46     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 47     | 5.000  |
+------------+--------+--------+
| 2016-12-19 | 48     | 5.000  |
+------------+--------+--------+

答案 1 :(得分:0)

这个不需要交叉连接:

select
    gs.d::date,
    generate_series(
        from_period, coalesce(to_period, from_period)
    ) as period,
    sum(sales) as sales
from
    look
    right join
    generate_series(
        current_date, current_date + 3, '1 day'
    ) gs (d) on d between from_date and to_date
group by 1, 2
order by 1, 2
;
     d      | period | sales  
------------+--------+--------
 2016-12-20 |     33 | 13.500
 2016-12-20 |     34 | 17.800
 2016-12-20 |     35 | 21.000
 2016-12-20 |     36 | 22.450
 2016-12-20 |     37 | 40.300
 2016-12-20 |     38 | 39.850
 2016-12-20 |     39 |  5.000
 2016-12-20 |     40 |  5.000
 2016-12-20 |     41 |  5.000
 2016-12-20 |     42 |  5.000
 2016-12-20 |     43 |  5.000
 2016-12-20 |     44 |  5.000
 2016-12-20 |     45 |  5.000
 2016-12-20 |     46 |  5.000
 2016-12-20 |     47 |  5.000
 2016-12-20 |     48 |  5.000