File size: 8,625 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
description,query
Get all records from fund_data,SELECT * FROM fund_data;
Get all records from fund_data,SELECT * FROM fund_data;
Get all unique fund names,SELECT DISTINCT scheme_name FROM fund_data;
Get the latest price for each fund,"
            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
            );
        "
Get the total sale_price_per_unit for each fund,"SELECT scheme_name, SUM(sale_price_per_unit) FROM fund_data GROUP BY scheme_name;"
Get the number of records for each fund,"SELECT scheme_name, COUNT(*) FROM fund_data GROUP BY scheme_name;"
Get the earliest and latest date_valued for each fund,"SELECT scheme_name, MIN(date_valued), MAX(date_valued) FROM fund_data GROUP BY scheme_name;"
Get the sale_price_per_unit for the latest date_valued for each fund,"
        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
        );
    "
Get the fund with the highest average sale_price_per_unit for each year,"
        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;
    "
Get the fund with the highest total sale_price_per_unit for each month in 2020,"
        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;
    "
Get the top 3 funds with the highest average sale_price_per_unit for each quarter in 2020,"
        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;
    "
Get the fund with the highest sale_price_per_unit for the last date_valued of each month in 2020,"
        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;
    "
Get the fund with the lowest average sale_price_per_unit,"
        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;
    "
Get the date with the highest total sale_price_per_unit for all funds,"
        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;
    "
Get the fund with the most records in the database,"
        SELECT scheme_name, COUNT(*) AS count
        FROM fund_data
        GROUP BY scheme_name
        ORDER BY count DESC
        LIMIT 1;
    "
Get the date with the most records in the database,"
        SELECT date_valued, COUNT(*) AS count
        FROM fund_data
        GROUP BY date_valued
        ORDER BY count DESC
        LIMIT 1;
    "
Get the fund with the highest sale_price_per_unit on the latest date_valued,"
        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;
    "
Get the percentage change in average sale_price_per_unit from the previous month for each fund in 2020,"
        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;
    "
Get the top 5 funds with the highest average sale_price_per_unit,"
        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;
    "
Calculate percentage return of funds in 2020,"
            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;
        "
Calculate Sharpe Ratio of Liquid Fund in 2020,"
            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;
        "
Get the total number of records in fund_data,SELECT COUNT(*) FROM fund_data;
Get the average sale_price_per_unit for each fund,"SELECT scheme_name, AVG(sale_price_per_unit) FROM fund_data GROUP BY scheme_name;"
Get all unique fund names,SELECT DISTINCT scheme_name FROM fund_data;
Get the latest price for each fund,"
            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
            );
        "
Calculate percentage return of funds in 2020,"
            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;
        "
Calculate Sharpe Ratio of Liquid Fund in 2020,"
            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;
        "