-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesser_queries.sql
182 lines (168 loc) · 3.26 KB
/
esser_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
.open "budget.db"
.mode csv
.echo off
.headers on
.print "Running report: ESSER budget totals by unit"
.once "output/esser_by_unit_and_year.csv"
SELECT
(everything.fiscal_year - 1) as fiscal_year,
everything.unit,
everything.unit_name,
total,
esser,
esser / total AS percentage
FROM (
SELECT
fiscal_year,
unit,
unit_name,
sum(last_ending_budget) AS total
FROM budget_book
GROUP BY fiscal_year, unit
ORDER BY fiscal_year DESC, total DESC
) AS everything
LEFT JOIN (
SELECT
fiscal_year,
unit,
unit_name,
IFNULL(SUM(last_ending_budget), 0.0) AS esser
FROM budget_book
WHERE fund LIKE "FG370%"
GROUP BY fiscal_year, unit
) AS covid
ON
covid.fiscal_year = everything.fiscal_year AND
covid.unit = everything.unit;
.once "output/esser_by_unit.csv"
SELECT
everything.unit,
everything.unit_name,
total,
esser,
esser / total AS percentage
FROM (
SELECT
unit,
unit_name,
sum(last_ending_budget) AS total
FROM budget_book
GROUP BY unit
ORDER BY total DESC
) AS everything
LEFT JOIN (
SELECT
unit,
unit_name,
IFNULL(SUM(last_ending_budget), 0.0) AS esser
FROM budget_book
WHERE fund LIKE "FG370%"
GROUP BY unit
) AS covid
ON
covid.unit = everything.unit;
.print "Running report: ESSER totals by account/program"
.once "output/esser_by_program_and_year.csv"
SELECT
(fiscal_year - 1) as fiscal_year,
account,
account_name,
program,
program_name,
total,
esser,
esser / total AS percentage
FROM (
SELECT
fiscal_year,
account,
account_name,
program,
program_name,
sum(last_ending_budget) AS total
FROM budget_book
GROUP BY fiscal_year, account, program
ORDER BY fiscal_year DESC, total DESC
) AS everything
LEFT JOIN (
SELECT
fiscal_year AS cfy,
account AS ca,
program AS cp,
IFNULL(SUM(last_ending_budget), 0.0) AS esser
FROM budget_book
WHERE fund LIKE "FG370%"
GROUP BY fiscal_year, account, program
) AS covid
ON
cfy = fiscal_year AND
ca = account AND
cp = program;
.once "output/esser_by_program.csv"
SELECT
account,
account_name,
program,
program_name,
total,
esser,
esser / total AS percentage
FROM (
SELECT
account,
account_name,
program,
program_name,
sum(last_ending_budget) AS total
FROM budget_book
GROUP BY account, program
ORDER BY total DESC
) AS everything
LEFT JOIN (
SELECT
account AS ca,
program AS cp,
IFNULL(SUM(last_ending_budget), 0.0) AS esser
FROM budget_book
WHERE fund LIKE "FG370%"
GROUP BY account, program
) AS covid
ON
ca = account AND
cp = program;
.once "output/esser_by_program_and_unit_prefix.csv"
SELECT
account,
account_name,
program,
program_name,
prefix,
total,
esser,
esser / total AS percentage
FROM (
SELECT
account,
account_name,
program,
program_name,
substring(unit, 0, 3) as prefix,
sum(last_ending_budget) AS total
FROM budget_book
GROUP BY account, program, prefix
ORDER BY total DESC
) AS everything
LEFT JOIN (
SELECT
account AS ca,
program AS cp,
substring(unit, 0, 3) as cu,
IFNULL(SUM(last_ending_budget), 0.0) AS esser
FROM budget_book
WHERE fund LIKE "FG370%"
GROUP BY ca, cp, cu
) AS covid
ON
ca = account AND
cp = program AND
cu = prefix;