-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
325 lines (294 loc) · 7.53 KB
/
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
\! echo "============================================="
\! echo "All-time service and project ordered by cost > x"
\! echo "============================================="
select
sum(c.cost),
s.service_name,
p.project_name
from
cost c,
project p,
service s
where
c.project_id = p.project_id and
s.service_id = c.service_id
group by
2,3
having
sum(c.cost) > 10
order by
1;
\! echo "============================================="
\! echo "All-time service ordered by cost > x"
\! echo "============================================="
select
sum(c.cost),
s.service_name
from
cost c,
service s
where
s.service_id = c.service_id
group by
s.service_name
having
sum(c.cost) > 10
order by
1;
\! echo "============================================="
\! echo "Cost per month"
\! echo "============================================="
select date_trunc('month', c.invoice_date) as month, sum(c.cost) as monthly_cost
from cost c
group by month
order by month;
\! echo "============================================="
\! echo "Cost per month trend per project"
\! echo "============================================="
select p.project_name, date_trunc('month', c.invoice_date) as month, sum(c.cost) as total_cost
from cost c
join project p on c.project_id = p.project_id
group by p.project_name, month
order by p.project_name, month;
\! echo "============================================="
\! echo "Cost per month trend per service"
\! echo "============================================="
select s.service_name, date_trunc('month', c.invoice_date) as month, sum(c.cost) as service_cost
from cost c
join service s on c.service_id = s.service_id
group by s.service_name, month
order by s.service_name, month;
\! echo "============================================="
\! echo "Cost per month trend per sku"
\! echo "============================================="
select sku.sku_name, date_trunc('month', c.invoice_date) as month, sum(c.usage_amount) as total_usage, sum(c.cost) as total_cost
from cost c
join sku on c.sku_id = sku.sku_id
group by sku.sku_name, month
order by sku.sku_name, month;
\! echo "============================================="
\! echo "Cost per month trend for specific project"
\! echo "============================================="
select s.service_name, date_trunc('month', c.invoice_date) as month, sum(c.cost) as total_cost
from cost c
join service s on c.service_id = s.service_id
where c.project_id = 'container-solutions-finance'
group by s.service_name, month
order by s.service_name, month;
\! echo "============================================="
\! echo "Savings through credits"
\! echo "============================================="
select credit_type, sum(c.cost) as saved_cost
from cost c
where c.credit_type is not null
group by credit_type
order by saved_cost desc;
\! echo "============================================="
\! echo "Usage and costs of skus"
\! echo "============================================="
select sku.sku_name, sum(c.usage_amount) as total_usage, sum(c.cost) as total_cost
from cost c
join sku on c.sku_id = sku.sku_id
group by sku.sku_name
order by total_cost desc;
\! echo "============================================="
\! echo "Project ordered by cost > x"
\! echo "============================================="
select
sum(c.cost),
p.project_name
from
cost c,
project p
where
c.project_id = p.project_id
group by
p.project_name
having
sum(c.cost) > 10
order by
1;
\! echo "============================================="
\! echo "All-time costs ordered by service and cost"
\! echo "============================================="
select
c.invoice_date,
c.cost,
c.usage_amount,
c.usage_unit,
p.project_name,
s.service_name
from
cost c,
project p,
service s
where
c.usage_amount is not null and
p.project_id = c.project_id and
s.service_id = c.service_id and
c.cost > 0
order by
c.cost desc,
s.service_name
limit 10;
\! echo "============================================="
\! echo "Show growth over time per project+service+sku"
\! echo "============================================="
-- Can adapt to make specific to project/service
with cost_lag as (
select
c.cost_id,
s.service_name,
p.project_name,
k.sku_name,
c.invoice_date,
c.credit_type,
c.cost_type,
c.sku_id,
c.usage_unit,
sum(cost) over (partition by c.project_id, c.service_id, c.sku_id, c.credit_type, c.cost_type order by c.invoice_date) as running_cost
from
cost c, service s, project p, sku k
where
c.service_id = s.service_id and
c.project_id = p.project_id and
c.sku_id = k.sku_id
order by
c.invoice_date,
s.service_name,
p.project_name,
k.sku_name
)
select
*
from
cost_lag
where
running_cost > 10
order by
running_cost desc
limit 100;
\! echo "============================================="
\! echo "Compare costs with previous month, showing any percentage change greater than x"
\! echo "============================================="
with cost_lag as (
select
c.cost_id,
s.service_name,
p.project_name,
k.sku_name,
c.invoice_date,
c.cost,
lag(cost) over (partition by c.project_id, c.service_id, c.sku_id order by c.invoice_date) as previous_month_running_costs
from
cost c, service s, project p, sku k
where
c.service_id = s.service_id and
c.project_id = p.project_id and
c.sku_id = k.sku_id
),
percent_change as (
select
*,
coalesce(round((cost - previous_month_running_costs) / coalesce(nullif(previous_month_running_costs,0), 1) * 100),0) as percent_change
from
cost_lag
)
select
*
from
percent_change
where
percent_change > 0
order by
percent_change desc
limit 10;
\! echo "============================================="
\! echo "As above, but per project"
\! echo "============================================="
with cost_per_service as (
select
sum(cost) as cost,
project_id,
invoice_date
from
cost
group by
2,3
),
lag_cost_per_service as (
select
cost,
project_id,
invoice_date,
lag(cost) over (partition by project_id order by invoice_date) as previous_month_running_costs
from
cost_per_service
),
project_percent_change as (
select
*,
coalesce(round((cost - previous_month_running_costs) / coalesce(nullif(previous_month_running_costs,0), 1) * 100),0) as percent_change
from
lag_cost_per_service
)
select
cost,
project_name,
invoice_date,
previous_month_running_costs,
percent_change
from
project_percent_change c,
project p
where
c.project_id = p.project_id and
c.percent_change > 0 and
c.invoice_date = '2024-01-31'
order by
c.percent_change desc
limit 10;
\! echo "============================================="
\! echo "As above, but per service"
\! echo "============================================="
with cost_per_project as (
select
sum(cost) as cost,
service_id,
invoice_date
from
cost
group by
2,3
),
lag_cost_per_project as (
select
cost,
service_id,
invoice_date,
lag(cost) over (partition by service_id order by invoice_date) as previous_month_running_costs
from
cost_per_project
),
service_percent_change as (
select
*,
coalesce(round((cost - previous_month_running_costs) / coalesce(nullif(previous_month_running_costs,0), 1) * 100),0) as percent_change
from
lag_cost_per_project
)
select
cost,
service_name,
invoice_date,
previous_month_running_costs,
percent_change
from
service_percent_change c, service s
where
s.service_id = c.service_id and
c.percent_change > 0 and
c.invoice_date = '2024-01-31'
order by
c.percent_change desc
limit 10;