Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- idclient | month_transac | sales
- ----------+---------------+--------
- 511656A75 | 2010-06-01 | 68.57
- 511656A75 | 2010-07-01 | 88.63
- 511656A75 | 2010-08-01 | 94.91
- 511656A75 | 2010-09-01 | 70.66
- 511656A75 | 2010-10-01 | 28.84
- [...]
- 511656A75 | 2015-10-01 | 85.00
- 511656A75 | 2015-12-01 | 114.42
- 511656A75 | 2016-01-01 | 137.08
- 511656A75 | 2016-03-01 | 172.92
- 511656A75 | 2016-04-01 | 125.00
- 511656A75 | 2016-05-01 | 127.08
- 511656A75 | 2016-06-01 | 104.17
- 511656A75 | 2016-07-01 | 98.22
- 511656A75 | 2016-08-01 | 37.08
- 511656A75 | 2016-10-01 | 108.33
- 511656A75 | 2016-11-01 | 104.17
- 511656A75 | 2017-01-01 | 201.67
- SELECT t1.idclient
- , t1.month_transac::date
- , t1.sales
- , SUM(t2.sales) as sales_ttm
- FROM temp_sales_sample_month_aggr t1
- LEFT JOIN temp_sales_sample_month_aggr t2 USING (idclient)
- WHERE
- t1.idclient = '511656A75' -- for example only
- AND t2.month_transac >= (t1.month_transac - interval '12 months')
- AND t2.month_transac < t1.month_transac
- GROUP BY 1, 2, 3
- ORDER BY 2
- ;
- idclient | month_transac | sales | sales_ttm
- -----------+---------------+--------+---------
- 511656A75 | 2010-07-01 | 88.63 | 68.57
- 511656A75 | 2010-08-01 | 94.91 | 157.20
- 511656A75 | 2010-09-01 | 70.66 | 252.11
- 511656A75 | 2010-10-01 | 28.84 | 322.77
- 511656A75 | 2010-11-01 | 110.38 | 351.61
- 511656A75 | 2010-12-01 | 125.67 | 461.99
- 511656A75 | 2011-01-01 | 108.45 | 587.66
- 511656A75 | 2011-02-01 | 83.21 | 696.11
- 511656A75 | 2011-03-01 | 102.73 | 779.32
- 511656A75 | 2011-04-01 | 254.09 | 882.05
- [...]
- idclient | month_transac | sales | sales_ttm
- -----------+---------------+--------+---------
- 511656A75 | 2010-06-01 | 68.57 | 0.00
- 511656A75 | 2010-07-01 | 88.63 | 68.57
- 511656A75 | 2010-08-01 | 94.91 | 157.20
- 511656A75 | 2010-09-01 | 70.66 | 252.11
- [...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement