-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats_aggregated_queries.sql
181 lines (157 loc) · 5.39 KB
/
stats_aggregated_queries.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
-- Play arround with queries for aggregated stats.
SELECT system_id, DATE_TRUNC('week', date) as time_period, ROUND(AVG(value))
FROM stats_pre_process
WHERE
date >= '2021-01-01' AND date <= '2021-12-09'
AND zone_ref = 'cbs:GM0599'
AND stat_description = 'number_of_vehicles_available'
GROUP BY system_id, DATE_TRUNC('week', date)
ORDER BY time_period;
SELECT system_id, DATE_TRUNC('month', date) as time_period, SUM(value)
FROM stats_pre_process
JOIN zones
ON stats_pre_process.zone_ref = zones.stats_ref
WHERE
date >= '2021-01-01' AND date <= '2021-12-09'
AND zone_id IN (34234)
AND stat_description = 'number_of_trip_started'
GROUP BY system_id, DATE_TRUNC('month', date)
ORDER BY time_period;
SELECT system_id, DATE_TRUNC('month', stat_dates) as time_period, SUM(value)
FROM (
SELECT generate_series('2021-01-01'::date, '2021-12-09'::date, '1d')::date AS stat_dates
) as t
LEFT JOIN stats_pre_process ON stat_dates = date
JOIN zones
ON stats_pre_process.zone_ref = zones.stats_ref
WHERE
date >= '2021-01-01' AND date <= '2021-12-09'
AND zone_id IN (34234)
AND system_id in ('baqme', 'gosharing')
AND stat_description = 'number_of_trip_started'
GROUP BY DATE_TRUNC('month', stat_dates), system_id
ORDER BY time_period;
WITH
time_serie AS (
SELECT generate_series('2021-01-01'::date, '2022-12-09'::date, '1d')::date AS stat_dates
),
stats AS (
SELECT system_id, DATE_TRUNC('day', stat_dates) as time_period, ROUND(AVG(value)) as value
FROM (
SELECT * FROM time_serie
) as t
LEFT JOIN stats_pre_process ON stat_dates = date
JOIN zones
ON stats_pre_process.zone_ref = zones.stats_ref
WHERE
date >= '2021-01-01' AND date <= '2022-12-09'
AND zone_id IN (34234)
AND system_id in ('baqme', 'gosharing', 'felyx', 'check')
AND stat_description = 'number_of_vehicles_available'
GROUP BY DATE_TRUNC('day', stat_dates), system_id
ORDER BY time_period
),
periods AS (
SELECT DISTINCT(DATE_TRUNC('day', stat_dates)) as time_period
FROM time_serie
),
operators AS (
SELECT DISTINCT(system_id)
FROM stats
)
SELECT periods.time_period, operators.system_id, coalesce(stats.value, 0)
FROM periods
CROSS JOIN operators
LEFT JOIN
stats ON
operators.system_id = stats.system_id
AND periods.time_period = stats.time_period
ORDER BY periods.time_period, operators.system_id;
SELECT system_id, DATE_TRUNC('month', stat_dates) as time_period, SUM(value)
FROM (
SELECT generate_series('2021-01-01'::date, '2021-12-09'::date, '1d')::date AS stat_dates
) as t
LEFT JOIN stats_pre_process ON stat_dates = date
JOIN zones
ON stats_pre_process.zone_ref = zones.stats_ref
WHERE
date >= '2021-01-01' AND date <= '2021-12-09'
AND zone_id IN (34234)
AND system_id in ('baqme', 'gosharing')
AND stat_description = 'number_of_trip_started'
GROUP BY DATE_TRUNC('month', stat_dates), system_id
ORDER BY time_period;
WITH
time_serie AS (
SELECT generate_series('2021-01-01'::date, '2022-12-09'::date, '1d')::date AS stat_dates
),
stats AS (
SELECT system_id, DATE_TRUNC('month', stat_dates) as time_period, SUM(value) as value
FROM (
SELECT * FROM time_serie
) as t
LEFT JOIN stats_pre_process ON stat_dates = date
JOIN zones
ON stats_pre_process.zone_ref = zones.stats_ref
WHERE
date >= '2021-01-01' AND date <= '2022-12-09'
AND zone_id IN (34234)
AND system_id in ('baqme', 'gosharing', 'felyx', 'check')
AND stat_description = 'number_of_trip_started'
GROUP BY DATE_TRUNC('month', stat_dates), system_id
ORDER BY time_period
),
periods AS (
SELECT DISTINCT(DATE_TRUNC('month', stat_dates)) as time_period
FROM time_serie
),
operators AS (
SELECT DISTINCT(system_id)
FROM stats
)
SELECT periods.time_period, operators.system_id, coalesce(stats.value, 0)
FROM periods
CROSS JOIN operators
LEFT JOIN
stats ON
operators.system_id = stats.system_id
AND periods.time_period = stats.time_period
ORDER BY periods.time_period, operators.system_id;
SELECT x.order_date, t.sale
x
LEFT JOIN sales t USING (order_date)
ORDER BY x.order_date;
SELECT q1.date
FROM
(SELECT date_trunc('day', dd)::date as date
FROM generate_series
( now()::date - %s, now()::date - 1, '1 day'::interval) dd) as q1
WHERE q1.date NOT IN (SELECT date
FROM stats_pre_process
WHERE stat_description = 'number_of_trips_ended');
SELECT t.system_id, DATE_TRUNC('month', stat_dates) as time_period, SUM(value)
FROM (
SELECT *
FROM
(
SELECT generate_series('2021-01-01'::date, '2021-12-09'::date, '1d') AS stat_dates
) AS q1,
(
SELECT distinct(system_id)
FROM stats_pre_process
WHERE
date >= '2021-01-01' AND date <= '2021-12-09'
and stat_description = 'number_of_trip_started'
) AS q2
) as t
LEFT JOIN stats_pre_process ON stat_dates = date
AND t.system_id = stats_pre_process.system_id
JOIN zones
ON stats_pre_process.zone_ref = zones.stats_ref
WHERE
date >= '2021-01-01' AND date <= '2021-12-09'
AND zone_id IN (34234)
AND t.system_id = 'baqme'
AND stat_description = 'number_of_trip_started'
GROUP BY t.system_id, DATE_TRUNC('month', stat_dates)
ORDER BY time_period;