-
Notifications
You must be signed in to change notification settings - Fork 0
/
AveragePercentGain.sql
81 lines (79 loc) · 1.97 KB
/
AveragePercentGain.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
USE stockdb
SELECT
MAXROW.strTick,
MAXROW.dtDate AS MAXDATE,
MAXROW.decClose AS MAXCLOSE,
MAXROW2.dtDate AS MAX2DATE,
MAXROW2.decClose AS MAX2CLOSE,
MINROW.dtDate AS MINDATE,
MINROW.decClose AS MINCLOSE,
CURGROUP.dtDate AS CURRDATE,
CURGROUP.decClose AS CURRCLOSE,
CURGROUP.decClose/MAXROW.decClose AS PCT_CHG_YOY,
ROWCT.NUMDAYS-252 AS NUMDAYS,
(MAXROW2.decClose/MINROW.decClose) AS PCT_CHG,
CAST((1/(ROWCT.NUMDAYS-((252.0*2)-1)))AS decimal(10,9)) AS EXPONENT,
CAST(POWER((MAXROW2.decClose/MINROW.decClose),CAST((1/(ROWCT.NUMDAYS-((252.0*2)-1)))AS decimal(10,9))) AS decimal(10,9)) AS DAILY_PCT_GAIN,
CAST(POWER((MAXROW.decClose/MINROW.decClose),CAST((1/(ROWCT.NUMDAYS-((252.0)-1)))AS decimal(10,9))) AS decimal(10,9)) AS CURR_DAILY_PCT_GAIN
FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY strTick ORDER BY dtDate DESC) AS ROWDATE,
*
FROM
snp500_test
) MAXROW
JOIN
(SELECT
ROW_NUMBER() OVER (PARTITION BY strTick ORDER BY dtDate DESC) AS ROWDATE,
*
FROM
snp500_test
) MAXROW2 ON MAXROW.strTick = MAXROW2.strTick
JOIN
(SELECT
ROW_NUMBER() OVER (PARTITION BY strTick ORDER BY dtDate ASC) AS ROWDATE,
*
FROM
snp500_test
) MINROW ON MINROW.strTick = MAXROW.strTick
JOIN
(
SELECT
strTick,
COUNT(*) AS NUMDAYS
FROM
snp500_test
GROUP BY
strTick
) ROWCT ON ROWCT.strTick = MINROW.strTick
AND ROWCT.strTick = MAXROW.strTick
JOIN
(
SELECT
dtDate,
strTick,
decClose
FROM
snp500_test snp
JOIN
(
SELECT
MAX(dtDate) AS MAXDATE,
strTick as tick
FROM
snp500_test
GROUP BY
strTick
) FST ON FST.MAXDATE = snp.dtDate
AND FST.tick = snp.strTick
) CURGROUP ON CURGROUP.strTick = MINROW.strTick
AND CURGROUP.strTick = MAXROW.strTick
AND CURGROUP.strTick = ROWCT.strTick
JOIN
CompanyList CL ON CL.strTick = MINROW.strTick
WHERE
MAXROW.ROWDATE = 252
AND MAXROW2.ROWDATE = (252*2)
AND MINROW.ROWDATE = 1
ORDER BY
CAST(POWER((MAXROW.decClose/MINROW.decClose),CAST((1/(ROWCT.NUMDAYS-253.0))AS decimal(10,9))) AS decimal(10,9)) DESC