Browse Source

Finish query for a processed April dataset

Petra Lamborn 5 years ago
parent
commit
2a1970250e
1 changed files with 70 additions and 1 deletions
  1. 70
    1
      sql/queries.pgsql

+ 70
- 1
sql/queries.pgsql View File

@@ -105,7 +105,8 @@ GROUP BY read_date, period
105 105
 ORDER BY read_date, period;
106 106
 
107 107
 -- Gerate timestamp list
108
-SELECT dd FROM GENERATE_SERIES('2017-04-01'::timestamp, '2017-04-02'::timestamp, '30 minutes'::interval) dd;
108
+SELECT read_date 
109
+FROM GENERATE_SERIES('2017-04-01'::timestamp, '2017-04-02'::timestamp, '30 minutes'::interval) read_date;
109 110
 
110 111
 -- Fraction of icp's in april with 1440 entries
111 112
 SELECT SUM(CASE WHEN isum.c = 1440 THEN 1 ELSE 0 END)::numeric / COUNT(*)::numeric As frac FROM
@@ -158,3 +159,71 @@ FROM
158 159
     GROUP BY icp_id
159 160
 ) AS cir 
160 161
 WHERE data_days >= 360;
162
+
163
+-- Gerate timestamp list for 2017
164
+SELECT read_date 
165
+FROM GENERATE_SERIES('2017-01-01 00:30:00'::timestamp, '2018-01-01 00:00:00'::timestamp, 
166
+    '30 minutes'::interval) read_date;
167
+
168
+
169
+-- --------------------------------
170
+-- Processed dataset for April 2017
171
+-- --------------------------------
172
+
173
+-- All combinations of read_time and icp_id for April
174
+SELECT read_time, icp_id
175
+FROM
176
+(
177
+    SELECT read_time 
178
+    FROM GENERATE_SERIES('2017-04-01 00:30:00'::timestamp, '2017-05-01 00:00:00'::timestamp, 
179
+        '30 minutes'::interval) read_time
180
+) AS tsdata CROSS JOIN
181
+(
182
+    SELECT *
183
+    FROM
184
+    (
185
+        SELECT icp_id, COUNT(DISTINCT read_date) AS data_days 
186
+        FROM coup_prd.coupdatamaster
187
+        WHERE read_date >= to_date('01/01/2017','dd/mm/yyyy')
188
+            AND read_date <  to_date('01/01/2018','dd/mm/yyyy')
189
+            AND content_code = 'UN'
190
+        GROUP BY icp_id
191
+    ) AS cir 
192
+    WHERE data_days >= 360
193
+) AS qual_icp;
194
+
195
+-- Creating timestamp column on April dataset
196
+SELECT *, read_date + CONCAT(period / 2, ':', period % 2 * 30, ':00')::time AS read_time
197
+FROM public.coup_tall_april;
198
+
199
+-- Combined query (for april only)
200
+SELECT comb.icp_id, comb.read_time, COALESCE(kwh_tot, 0) AS kwh_tot
201
+FROM
202
+(
203
+    SELECT read_time, icp_id
204
+    FROM
205
+    (
206
+        SELECT read_time 
207
+        FROM GENERATE_SERIES('2017-04-01 00:30:00'::timestamp, '2017-05-01 00:00:00'::timestamp, 
208
+            '30 minutes'::interval) read_time
209
+    ) AS tsdata CROSS JOIN
210
+    (
211
+        SELECT *
212
+        FROM
213
+        (
214
+            SELECT icp_id, COUNT(DISTINCT read_date) AS data_days 
215
+            FROM coup_prd.coupdatamaster
216
+            WHERE read_date >= to_date('01/01/2017','dd/mm/yyyy')
217
+                AND read_date <  to_date('01/01/2018','dd/mm/yyyy')
218
+                AND content_code = 'UN'
219
+            GROUP BY icp_id
220
+        ) AS cir 
221
+        WHERE data_days >= 360
222
+    ) AS qual_icp
223
+) AS comb
224
+LEFT JOIN
225
+(
226
+    SELECT *, read_date + CONCAT(period / 2, ':', period % 2 * 30, ':00')::time AS read_time
227
+    FROM public.coup_tall_april
228
+) AS tall_timestamp 
229
+ON comb.read_time = tall_timestamp.read_time AND comb.icp_id = tall_timestamp.icp_id;