This is a collect of information on useful SQL commands for looking at the database and for using with testing for correct functioning. It can allow for loading of special data to see how it functions. It also shows SQL commands that my be interesting and useful in other contexts and modifiable.
It is provided for the record and for potential use. Some items may be out of date or have issues as they have not been retested. Some assume the basic test data has been loaded.
This is only the first version with limited information. More should be coming in the future. If you have ideas for additions/changes then please make a pull request.
-- see data
select * from meters;
select * from units;
select * from week_patterns;
select * from day_patterns;
select * from day_segments;
select * from conversions;
select * from conversion_segments;
select * from cik;
select * from cik_vary order by destination_id, start_time;
-- Remove data
delete from meters;
delete from conversion_segments;
delete from conversions;
delete from week_patterns;
delete from day_patterns;
delete from day_segments;
delete from cik;
delete from cik_vary;
delete from units;
-- Week 1
-- day pattern
insert into day_patterns values (DEFAULT, 'dp1', 'for ds2, ds3, ds5');
insert into day_patterns values (DEFAULT, 'dp2', 'for ds7');
-- day segments
insert into day_segments values (DEFAULT, ( SELECT id FROM day_patterns WHERE NAME = 'dp1' ), 0, 7, 2, 0, 'd2 for dp1 0-7 slope 2');
insert into day_segments values (DEFAULT, ( SELECT id FROM day_patterns WHERE NAME = 'dp1' ), 7, 23, 3, 0, 'd3 for dp1 7-23 slope 3');
insert into day_segments values (DEFAULT, ( SELECT id FROM day_patterns WHERE NAME = 'dp1' ), 23, 24, 5, 0, 'd5 for dp1 23-24 slope 5');
insert into day_segments values (DEFAULT, ( SELECT id FROM day_patterns WHERE NAME = 'dp2' ), 0, 24, 7, 0, 'd7 for dp2 0-24 slope 7');
--- week pattern
insert into week_patterns values (DEFAULT, 'w1', 'dp1: M, F; dp2: N, T, W, R, S', ( SELECT id FROM day_patterns WHERE NAME = 'dp2' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp1' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp2' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp2' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp2' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp1' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp2' ) );
-- units
INSERT INTO units(name, identifier, unit_represent, sec_in_rate, type_of_unit, suffix, displayable, preferred_display, note, min_val, max_val, disable_checks) VALUES ('um1', 'um1', 'quantity', 3600, 'meter', '', 'none', false, 'um1 meter unit', -999999999, 999999999, 'reject_all');
INSERT INTO units(name, identifier, unit_represent, sec_in_rate, type_of_unit, suffix, displayable, preferred_display, note, min_val, max_val, disable_checks) VALUES ('ug2', 'ug2', 'quantity', 3600, 'unit', '', 'all', false, 'ug2 graphic unit', -999999999, 999999999, 'reject_all');
INSERT INTO units(name, identifier, unit_represent, sec_in_rate, type_of_unit, suffix, displayable, preferred_display, note, min_val, max_val, disable_checks) VALUES ('ug3', 'ug3', 'quantity', 3600, 'unit', '', 'all', false, 'ug3 graphic unit', -999999999, 999999999, 'reject_all');
-- conversions
INSERT INTO CONVERSIONS VALUES ( ( SELECT id FROM units WHERE NAME = 'um1' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug2' ), false, 'um1 -> ug2' );
INSERT INTO CONVERSIONS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug2' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug3' ), true, 'ug2 <-> ug3' );
-- conversion segments
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'um1' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug2' ), NULL, 10, 0, '-infinity', '2020-06-03', 'segment 1 slope 10 um1 -> ug2' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'um1' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug2' ), (select id from week_patterns where name = 'w1'), 0, 0, '2020-06-03', '2020-06-07', 'segment 2 week 1 um1 <-> ug2' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'um1' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug2' ), null, 20, 0, '2020-06-07', '2020-06-16', 'segment 3 slope 20 um1 -> ug2' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'um1' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug2' ), null, 30, 0, '2020-06-16', 'infinity', 'segment 4 slope 30 um1 -> ug2' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug2' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug3' ), NULL, 40, 0, '-infinity', '2020-06-04', 'segment 1 slope 40 ug2 <-> ug3' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug2' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug3' ), NULL, 50, 0, '2020-06-04', '2020-06-10', 'segment 2 slope 50 ug2 <-> ug3' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug2' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug3' ), null, 60, 0, '2020-06-10', '2020-06-17', 'segment 3 slope 60 ug2 <-> ug3' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug2' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug3' ), null, 70, 0, '2020-06-17', 'infinity', 'segment 4 slope 70 ug2 <-> ug3' );
-- Week 2: need to do week 1 first
-- day pattern
insert into day_patterns values (DEFAULT, 'dp3', 'for ds13, ds17');
-- day segments
insert into day_segments values (DEFAULT, ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), 0, 12, 13, 0, 'd13 for dp3 0-12 slope 13');
insert into day_segments values (DEFAULT, ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), 12, 24, 17, 0, 'd17 for dp3 12-24 slope 17');
--- week pattern
insert into week_patterns values (DEFAULT, 'w2', 'dp3: all days', ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ), ( SELECT id FROM day_patterns WHERE NAME = 'dp3' ) );
-- units
INSERT INTO units(name, identifier, unit_represent, sec_in_rate, type_of_unit, suffix, displayable, preferred_display, note, min_val, max_val, disable_checks) VALUES ('ug4', 'ug4', 'quantity', 3600, 'unit', '', 'all', false, 'ug4 graphic unit', -999999999, 999999999, 'reject_all');
-- conversions
INSERT INTO CONVERSIONS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug3' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug4' ), true, 'ug3 <-> ug4' );
-- conversion segments
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug3' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug4' ), NULL, 80, 0, '-infinity', '2020-06-06', 'segment 1 slope 80 ug3 <-> ug4' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug3' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug4' ), (select id from week_patterns where name = 'w2'), 0, 0, '2020-06-06', '2020-06-17', 'segment 2 week 2 ug3 <-> ug4' );
INSERT INTO CONVERSION_SEGMENTS VALUES ( ( SELECT id FROM units WHERE NAME = 'ug3' ), ( SELECT ID FROM UNITS WHERE NAME = 'ug4' ), null, 90, 0, '2020-06-17', 'infinity', 'segment 3 slope 90 ug3 <-> ug4' );
-- meter: Not be formally needed but then can add readings when want.
INSERT INTO meters(name, url, enabled, displayable, meter_type, default_timezone_meter, gps, identifier, note, area, cumulative, cumulative_reset, cumulative_reset_start, cumulative_reset_end, reading_gap, reading_variation, reading_duplication, time_sort, end_only_time, reading, start_timestamp, end_timestamp, previous_end, unit_id, default_graphic_unit, area_unit, reading_frequency, min_val, max_val, min_date, max_date, max_error, disable_checks) VALUES ('m1', null, false, true, 'other', DEFAULT, DEFAULT, 'm1', 'meter 1', DEFAULT, false, false, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, ( SELECT ID FROM UNITS WHERE NAME = 'um1' ), null, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT);