Browse Source

Dedicated SQL file

Petra Lamborn 5 years ago
parent
commit
5fce736d93
1 changed files with 99 additions and 0 deletions
  1. 99
    0
      sql/queries.pgsql

+ 99
- 0
sql/queries.pgsql View File

@@ -0,0 +1,99 @@
1
+-- Create "tall" table for april 2017 (from Jason) 
2
+CREATE TABLE public.coup_tall_april AS
3
+SELECT  a.icp_id
4
+     , a.read_date
5
+     , c.period
6
+     , sum(c.read_kwh) as kwh_tot
7
+     , sum(case when a.content_code = 'UN' then c.read_kwh else 0 end) as kwh_un
8
+     , sum(case when a.content_code in ('CN','EG') then c.read_kwh else 0 end) as kwh_cn
9
+FROM    coup_prd.coupdatamaster a,
10
+	unnest(a.read_array) WITH ORDINALITY c(read_kwh, period)
11
+WHERE   a.read_date >= to_date('01/04/2017','dd/mm/yyyy')
12
+ and   a.read_date <  to_date('01/05/2017','dd/mm/yyyy')
13
+ and   a.content_code  ~ ('UN|CN|EG')
14
+GROUP BY 1, 2, 3
15
+ORDER BY 1, 2, 3;
16
+
17
+-- "Head" of the data
18
+SELECT * FROM public.coup_tall_april limit 10;
19
+
20
+-- Count distinct ICPs for each date
21
+SELECT read_date, COUNT(DISTINCT icp_id) AS d_icp 
22
+FROM public.coup_tall_april 
23
+GROUP BY read_date;
24
+
25
+-- Calculate mininmums, maximums, averages for each date
26
+SELECT read_date, MIN(kwh_tot), AVG(kwh_tot), MAX(kwh_tot) 
27
+FROM public.coup_tall_april 
28
+GROUP BY read_date;
29
+
30
+-- Find negative values
31
+SELECT * FROM public.coup_tall_april WHERE kwh_tot < 0 OR kwh_un < 0 OR kwh_cn < 0;
32
+
33
+-- Count number of ICPs that end in "17"
34
+SELECT COUNT (DISTINCT icp_id) FROM public.coup_tall_april WHERE icp_id LIKE '%17';
35
+
36
+-- List first 10 ICPs that end with number 17, sorted
37
+SELECT DISTINCT icp_id FROM public.coup_tall_april WHERE icp_id LIKE '%17' ORDER BY icp_id LIMIT 10;
38
+
39
+-- Verify that all ICP IDs are of length 7
40
+SELECT MIN(CHAR_LENGTH(icp_id)) as minl, MAX(CHAR_LENGTH(icp_id)) as maxl FROM public.coup_tall_april;
41
+
42
+-- String conversion
43
+SELECT DISTINCT CAST(SUBSTRING(icp_id FROM 2 FOR 6) AS int) AS icp_val, icp_id FROM public.coup_tall_april 
44
+
45
+-- Count nulls in cast (none)
46
+SELECT COUNT(*) - COUNT(CAST(SUBSTRING(icp_id FROM 2 FOR 6) AS int)) FROM public.coup_tall_april;
47
+
48
+-- Verify that there are only "period" values from 1 to 48
49
+SELECT DISTINCT period FROM public.coup_tall_april ORDER BY period;
50
+
51
+-- Look at the 2nd specifically (DST ends, 2017)
52
+SELECT * FROM public.coup_tall_april 
53
+WHERE read_date = to_date('02/04/2017', 'dd/mm/yy') 
54
+ORDER BY icp_id, period 
55
+LIMIT 60;
56
+
57
+-- September interlude STARTS
58
+-- Create "tall" table for Septeber 24 2017 (mod from Jason, above) to look at DST starting
59
+CREATE TABLE public.coup_dst_2017 AS
60
+SELECT  a.icp_id
61
+     , a.read_date
62
+     , c.period
63
+     , sum(c.read_kwh) as kwh_tot
64
+     , sum(case when a.content_code = 'UN' then c.read_kwh else 0 end) as kwh_un
65
+     , sum(case when a.content_code in ('CN','EG') then c.read_kwh else 0 end) as kwh_cn
66
+FROM    coup_prd.coupdatamaster a,
67
+	unnest(a.read_array) WITH ORDINALITY c(read_kwh, period)
68
+WHERE   a.read_date = to_date('24/09/2017','dd/mm/yyyy')
69
+ and   a.content_code  ~ ('UN|CN|EG')
70
+GROUP BY 1, 2, 3
71
+ORDER BY 1, 2, 3;
72
+
73
+-- Look at September 24 2017 table
74
+SELECT * FROM public.coup_dst_2017 
75
+ORDER BY icp_id, period 
76
+LIMIT 60;
77
+
78
+-- Verify that there are only "period" values from 1 to 48
79
+SELECT COUNT(DISTINCT period) FROM public.coup_dst_2017;
80
+-- September interlude ENDS
81
+
82
+-- Timestamp conversion
83
+SELECT DISTINCT DATE_PART('year', read_date), DATE_PART('month', read_date), 
84
+    DATE_PART('day', read_date) FROM public.coup_tall_april ORDER BY 1, 2, 3;
85
+
86
+-- Creating "time" values
87
+SELECT *, (CONCAT(q.h, ':', q.m, ':00'))::time AS t FROM
88
+(
89
+    SELECT DISTINCT period, period / 2 as h, period % 2 * 30 AS m
90
+    FROM public.coup_tall_april
91
+) AS q ORDER BY period;
92
+
93
+-- Creating timestamp column
94
+SELECT *, read_date + CONCAT(period / 2, ':', period % 2 * 30, ':00')::time AS read_time
95
+FROM public.coup_tall_april ORDER BY icp_id, read_time limit 50;
96
+
97
+-- Possible subset for further testing
98
+SELECT *, read_date + CONCAT(period / 2, ':', period % 2 * 30, ':00')::time AS read_time
99
+FROM public.coup_tall_april WHERE icp_id LIKE '%1117' ORDER BY icp_id, read_time;