-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweekly-report.sql
65 lines (63 loc) · 1.56 KB
/
weekly-report.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
\set QUIET 1
\timing
\pset footer off
\x off
\set QUIET 0
SELECT
DISTINCT (data -> 'tags' ->> 'id') as "DeviceID",
(data ->> 'measurement') as "Low Battery Device"
FROM weather
WHERE
(data -> 'tags' ->> 'battery_low')::boolean = true
AND
((data ->> 'time'))::timestamptz BETWEEN now() - interval '5d' AND now();
--with series as (
-- SELECT * FROM (
-- select
-- (data -> 'fields' ->> 'temperature_F')::decimal as n
-- from weather
-- where
-- --(data -> 'tags' ->> 'id') like '9359'
-- (data ->> 'measurement') like 'Acurite 3n1%'
-- and
-- (data ->> 'time')::timestamptz BETWEEN now() - '24 hours'::interval AND now() - interval '12h'
-- order by date_trunc('hour', (data ->> 'time')::timestamptz) desc limit 1000
--) AS n),
--bounds AS (
-- SELECT
-- avg(n) - stddev(n) AS lower_bound,
-- avg(n) + stddev(n) AS upper_bound
-- FROM
-- series
--),
--stats AS (
-- SELECT
-- avg(n) series_mean,
-- stddev(n) as series_stddev
-- FROM
-- series
--),
--zscores AS (
-- SELECT
-- series.n,
-- (series.n - series_mean) / series_stddev AS zscore
-- FROM
-- series,
-- stats
--)
--SELECT
-- series.n,
-- lower_bound,
-- upper_bound,
-- (series.n - series_mean) / series_stddev as zscore,
-- series.n NOT BETWEEN lower_bound AND upper_bound AS is_anomaly,
-- zscore NOT BETWEEN -1.5 AND 1.5 AS zscore_anomaly
--FROM
-- series,
-- stats,
-- zscores,
-- bounds
-- WHERE
-- series.n NOT BETWEEN lower_bound AND upper_bound = true
-- LIMIT 100;
--