@@ -85,9 +85,9 @@ function printTable(array $data, array $footer = []){
8585 TRUNC(100 * (s.generation - bar.out) / (bar.in - bar.out + s.generation), 2) as self_suff
8686FROM
8787 (SELECT
88- *,
89- in_1+in_2 as in ,
90- out_1+out_2 as out
88+ foo.*,
89+ foo. in_1+foo. in_2 as in ,
90+ foo. out_1+foo. out_2 as out
9191 FROM
9292 (SELECT
9393 MIN(sample) as start,
@@ -96,21 +96,42 @@ function printTable(array $data, array $footer = []){
9696 MAX(kwh_in_2) - MIN(kwh_in_2) as in_2,
9797 MAX(kwh_out_1) - MIN(kwh_out_1) as out_1,
9898 MAX(kwh_out_2) - MIN(kwh_out_2) as out_2
99- FROM electricity
99+ FROM
100+ electricity
100101 /* start date of Powerpeers subscription */
101- WHERE date(sample) > '2018-07-11'
102+ WHERE
103+ date(sample) > '2018-07-11'
102104 GROUP BY
103105 to_char(sample, 'YYYY-MM')
104- ORDER BY MIN(sample)
106+ ORDER BY
107+ MIN(sample)
105108 ) as foo
109+ LEFT JOIN (SELECT
110+ MAX(sample) - INTERVAL '1 month' as end,
111+ MAX(kwh_in_1) as in_1,
112+ MAX(kwh_in_2) as in_2,
113+ MAX(kwh_out_1) as out_1,
114+ MAX(kwh_out_2) as out_2
115+ FROM electricity
116+ WHERE date(sample) > '2018-07-11'
117+ GROUP BY
118+ to_char(sample, 'YYYY-MM')
119+ ORDER BY MIN(sample)) as prev ON prev.end=foo.end
120+
106121 ) as bar
107122 JOIN (SELECT
108- to_char(sample, 'YYYY-MM') AS yearmonth,
109- MAX(yield_total) - MIN(yield_total) as generation
110- FROM solax s
123+ to_char(daily.sample, 'YYYY-MM') AS yearmonth,
124+ SUM(daily.yield) as generation
125+ FROM (SELECT
126+ MAX(sd.sample) as sample,
127+ MAX(sd.yield_today) as yield
128+ FROM solax sd
129+ WHERE date(sd.sample) > '2018-07-11'
130+ GROUP BY DATE(sd.sample)
131+ ) as daily
111132 WHERE date(sample) > '2018-07-11'
112133 GROUP BY
113- to_char(sample, 'YYYY-MM')
134+ to_char(daily. sample, 'YYYY-MM')
114135
115136 ) as s ON s.yearmonth = to_char(bar.start, 'YYYY-MM')
116137 ORDER BY bar.start
0 commit comments