File size: 10,494 Bytes
ab1d89e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv

queries = [
    {
        "description": "Get all records from fund_data",
        "query": "SELECT * FROM fund_data;",
    },
    {
        "description": "Get all records from fund_data",
        "query": "SELECT * FROM fund_data;",
    },
    {
        "description": "Get all unique fund names",
        "query": "SELECT DISTINCT scheme_name FROM fund_data;",
    },
    {
        "description": "Get the latest price for each fund",
        "query": """
            SELECT scheme_name, sale_price_per_unit
            FROM fund_data
            WHERE (scheme_name, date_valued) IN (
                SELECT scheme_name, MAX(date_valued)
                FROM fund_data
                GROUP BY scheme_name
            );
        """,
    },
    {
        "description": "Get the total sale_price_per_unit for each fund",
        "query": "SELECT scheme_name, SUM(sale_price_per_unit) FROM fund_data GROUP BY scheme_name;",
    },
    {
        "description": "Get the number of records for each fund",
        "query": "SELECT scheme_name, COUNT(*) FROM fund_data GROUP BY scheme_name;",
    },
    {
        "description": "Get the earliest and latest date_valued for each fund",
        "query": "SELECT scheme_name, MIN(date_valued), MAX(date_valued) FROM fund_data GROUP BY scheme_name;",
    },
    {
        "description": "Get the sale_price_per_unit for the latest date_valued for each fund",
        "query": """
        SELECT scheme_name, sale_price_per_unit
        FROM fund_data
        WHERE date_valued = (
            SELECT MAX(date_valued)
            FROM fund_data AS fd
            WHERE fd.scheme_name = fund_data.scheme_name
        );
    """,
    },
    {
        "description": "Get the fund with the highest average sale_price_per_unit for each year",
        "query": """
        SELECT YEAR(date_valued) AS year, scheme_name, AVG(sale_price_per_unit) AS avg_price
        FROM fund_data
        GROUP BY year, scheme_name
        ORDER BY avg_price DESC;
    """,
    },
    {
        "description": "Get the fund with the highest total sale_price_per_unit for each month in 2020",
        "query": """
        SELECT MONTH(date_valued) AS month, scheme_name, SUM(sale_price_per_unit) AS total_price
        FROM fund_data
        WHERE YEAR(date_valued) = 2020
        GROUP BY month, scheme_name
        ORDER BY total_price DESC;
    """,
    },
    {
        "description": "Get the top 3 funds with the highest average sale_price_per_unit for each quarter in 2020",
        "query": """
        SELECT QUARTER(date_valued) AS quarter, scheme_name, AVG(sale_price_per_unit) AS avg_price
        FROM fund_data
        WHERE YEAR(date_valued) = 2020
        GROUP BY quarter, scheme_name
        ORDER BY avg_price DESC
        LIMIT 3;
    """,
    },
    {
        "description": "Get the fund with the highest sale_price_per_unit for the last date_valued of each month in 2020",
        "query": """
        SELECT scheme_name, sale_price_per_unit
        FROM fund_data
        WHERE date_valued IN (
            SELECT MAX(date_valued)
            FROM fund_data
            WHERE YEAR(date_valued) = 2020
            GROUP BY MONTH(date_valued)
        )
        ORDER BY sale_price_per_unit DESC;
    """,
    },
    {
        "description": "Get the fund with the lowest average sale_price_per_unit",
        "query": """
        SELECT scheme_name, AVG(sale_price_per_unit) AS avg_price
        FROM fund_data
        GROUP BY scheme_name
        ORDER BY avg_price ASC
        LIMIT 1;
    """,
    },
    {
        "description": "Get the date with the highest total sale_price_per_unit for all funds",
        "query": """
        SELECT date_valued, SUM(sale_price_per_unit) AS total_price
        FROM fund_data
        GROUP BY date_valued
        ORDER BY total_price DESC
        LIMIT 1;
    """,
    },
    {
        "description": "Get the fund with the most records in the database",
        "query": """
        SELECT scheme_name, COUNT(*) AS count
        FROM fund_data
        GROUP BY scheme_name
        ORDER BY count DESC
        LIMIT 1;
    """,
    },
    {
        "description": "Get the date with the most records in the database",
        "query": """
        SELECT date_valued, COUNT(*) AS count
        FROM fund_data
        GROUP BY date_valued
        ORDER BY count DESC
        LIMIT 1;
    """,
    },
    {
        "description": "Get the fund with the highest sale_price_per_unit on the latest date_valued",
        "query": """
        SELECT scheme_name, sale_price_per_unit
        FROM fund_data
        WHERE date_valued = (
            SELECT MAX(date_valued)
            FROM fund_data
        )
        ORDER BY sale_price_per_unit DESC
        LIMIT 1;
    """,
    },
    {
        "description": "Get the percentage change in average sale_price_per_unit from the previous month for each fund in 2020",
        "query": """
        SELECT scheme_name, MONTH(date_valued) AS month, 
        ((AVG(sale_price_per_unit) - LAG(AVG(sale_price_per_unit)) OVER (PARTITION BY scheme_name ORDER BY month)) / LAG(AVG(sale_price_per_unit)) OVER (PARTITION BY scheme_name ORDER BY month)) * 100 AS pct_change
        FROM fund_data
        WHERE YEAR(date_valued) = 2020
        GROUP BY scheme_name, month;
    """,
    },
    {
        "description": "Get the top 5 funds with the highest average sale_price_per_unit",
        "query": """
        SELECT scheme_name, AVG(sale_price_per_unit) AS avg_price
        FROM fund_data
        GROUP BY scheme_name
        ORDER BY avg_price DESC
        LIMIT 5;
    """,
    },
    {
        "description": "Calculate percentage return of funds in 2020",
        "query": """
            SELECT 
                start_price.scheme_name, 
                ((end_price.sale_price_per_unit - start_price.sale_price_per_unit) / start_price.sale_price_per_unit) * 100 AS percentage_return
            FROM 
                (SELECT scheme_name, sale_price_per_unit
                FROM fund_data
                WHERE scheme_name IN ('Liquid Fund', 'Bond Fund') AND date_valued = (SELECT MIN(date_valued) FROM fund_data WHERE scheme_name = fund_data.scheme_name)
                GROUP BY scheme_name) AS start_price,
                (SELECT scheme_name, sale_price_per_unit
                FROM fund_data
                WHERE scheme_name IN ('Liquid Fund', 'Bond Fund') AND date_valued = (SELECT MAX(date_valued) FROM fund_data WHERE scheme_name = fund_data.scheme_name)
                GROUP BY scheme_name) AS end_price
            WHERE start_price.scheme_name = end_price.scheme_name;
        """,
    },
    {
        "description": "Calculate Sharpe Ratio of Liquid Fund in 2020",
        "query": """
            SELECT 
                scheme_name,
                AVG(daily_return) AS avg_return,
                STDDEV_POP(daily_return) AS std_dev,
                (AVG(daily_return) - 12) / STDDEV_POP(daily_return) AS sharpe_ratio
            FROM 
                (SELECT 
                    scheme_name,
                    ((sale_price_per_unit - LAG(sale_price_per_unit) OVER (PARTITION BY scheme_name ORDER BY date_valued)) / LAG(sale_price_per_unit) OVER (PARTITION BY scheme_name ORDER BY date_valued)) * 100 AS daily_return
                FROM 
                    fund_data
                WHERE 
                    scheme_name = 'Liquid Fund' AND YEAR(date_valued) = 2020) AS daily_returns
            GROUP BY 
                scheme_name;
        """,
    },
    {
        "description": "Get the total number of records in fund_data",
        "query": "SELECT COUNT(*) FROM fund_data;",
    },
    {
        "description": "Get the average sale_price_per_unit for each fund",
        "query": "SELECT scheme_name, AVG(sale_price_per_unit) FROM fund_data GROUP BY scheme_name;",
    },
    {
        "description": "Get all unique fund names",
        "query": "SELECT DISTINCT scheme_name FROM fund_data;",
    },
    {
        "description": "Get the latest price for each fund",
        "query": """
            SELECT scheme_name, sale_price_per_unit
            FROM fund_data
            WHERE (scheme_name, date_valued) IN (
                SELECT scheme_name, MAX(date_valued)
                FROM fund_data
                GROUP BY scheme_name
            );
        """,
    },
    {
        "description": "Calculate percentage return of funds in 2020",
        "query": """
            SELECT 
                start_price.scheme_name, 
                ((end_price.sale_price_per_unit - start_price.sale_price_per_unit) / start_price.sale_price_per_unit) * 100 AS percentage_return
            FROM 
                (SELECT scheme_name, sale_price_per_unit
                FROM fund_data
                WHERE scheme_name IN ('Liquid Fund', 'Bond Fund') AND date_valued = (SELECT MIN(date_valued) FROM fund_data WHERE scheme_name = fund_data.scheme_name)
                GROUP BY scheme_name) AS start_price,
                (SELECT scheme_name, sale_price_per_unit
                FROM fund_data
                WHERE scheme_name IN ('Liquid Fund', 'Bond Fund') AND date_valued = (SELECT MAX(date_valued) FROM fund_data WHERE scheme_name = fund_data.scheme_name)
                GROUP BY scheme_name) AS end_price
            WHERE start_price.scheme_name = end_price.scheme_name;
        """,
    },
    {
        "description": "Calculate Sharpe Ratio of Liquid Fund in 2020",
        "query": """
            SELECT 
                scheme_name,
                AVG(daily_return) AS avg_return,
                STDDEV_POP(daily_return) AS std_dev,
                (AVG(daily_return) - 12) / STDDEV_POP(daily_return) AS sharpe_ratio
            FROM 
                (SELECT 
                    scheme_name,
                    ((sale_price_per_unit - LAG(sale_price_per_unit) OVER (PARTITION BY scheme_name ORDER BY date_valued)) / LAG(sale_price_per_unit) OVER (PARTITION BY scheme_name ORDER BY date_valued)) * 100 AS daily_return
                FROM 
                    fund_data
                WHERE 
                    scheme_name = 'Liquid Fund' AND YEAR(date_valued) = 2020) AS daily_returns
            GROUP BY 
                scheme_name;
        """,
    },
]

with open("queries.csv", "w", newline="") as csvfile:
    fieldnames = ["description", "query"]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for query in queries:
        writer.writerow(query)