-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL context.sql
More file actions
78 lines (69 loc) · 2.01 KB
/
Copy pathSQL context.sql
File metadata and controls
78 lines (69 loc) · 2.01 KB
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
use stock_analysis;
create table if not exists user_ (Date DATETIME,Open FLOAT,High FLOAT,Low float,Close float,Adj_Close float, Volume INT);
-- Best return day
with cte1 as (
select *,(LAG(CLOSE) OVER(ORDER BY DATE)-close)/close as daily_Return
from aa
)
select date,round(daily_return,2) as Highest_return
from cte1
where daily_return = (select max(daily_return) from cte1);
-- Lowest return day
with cte2 as (
select *,(LAG(CLOSE) OVER(ORDER BY DATE)-close)/close as daily_Return
from aa
)
select date,round(daily_return,2) as Lowest_return
from cte2
where daily_return = (select min(daily_return) from cte2);
-- First Day which less than Average return day
with cte3 as (
select *,(LAG(CLOSE) OVER(ORDER BY DATE)-close)/close as daily_Return
from aal
)
select date,round(daily_return,5)
from cte3
where daily_return <= (select avg(daily_return) from cte3)
order by daily_return desc
limit 1;
-- Query for Volatility
with cte4 as (
select *, (lag(close) over(order by date) - close)/close as daily_return
from aal),
cte5 as (
select year(date) as year,round((std(daily_Return))*100,2) as volatility
from cte4
group by year(date)),
cte6 as (
select year,volatility,row_number() over(order by volatility) as rnk
from cte5)
select *
from cte6
where (rnk = 1) or (rnk = 2) or (rnk > (select count(*)-2 from cte5))
;
-- Yearly Returns
with cte9 as (
select year(date) as Year_,close
from aal
group by year(date),close),
cte10 as (
select *,row_number() over(partition by Year_) as rnk from cte9),
cte11 as (
select Year_,max(close) as highest_close
from cte10
group by Year_)
select Year_, (lag(highest_close) over(order by Year_) - highest_close)/highest_close as yearly_return
from cte11
limit 655555555555535 offset 1;
with cte as
(select *,(LAG(CLOSE) OVER(ORDER BY DATE)-close)/close as daily_Return
from aal),
cte12 as (
select date,round(daily_return,3)
from cte
where daily_return = (select max(daily_return) from cte) or
(daily_return = (select min(daily_return) from cte))
order by daily_return desc
)
select * from cte12;
select * from aal;