-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_views.sql
More file actions
88 lines (72 loc) · 2.65 KB
/
Copy path_views.sql
File metadata and controls
88 lines (72 loc) · 2.65 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
79
80
81
82
83
84
85
CREATE VIEW industrial_countries
AS
SELECT location, COUNT(symbol) as num_of_countries
FROM company
WHERE founded<1990
group by location
HAVING count(symbol) >5;
CREATE VIEW high_stocks
AS
SELECT company.location, stock.belongsTo, stock.stockValue
FROM company, stock
WHERE company.symbol=stock.belongsTo;
CREATE VIEW max_stocks_by_country
AS
SELECT high_stocks.location, max(high_stocks.stockValue) as max_stock
FROM high_stocks
GROUP BY high_stocks.location;
CREATE VIEW max_stocks_company_by_country
AS
SELECT DISTINCT high_stocks.belongsTo, max_stocks_by_country.max_stock, high_stocks.location
FROM max_stocks_by_country, high_stocks
WHERE max_stocks_by_country.max_stock = high_stocks.stockValue and high_stocks.location = max_stocks_by_country.location;
CREATE VIEW not_improve_companies
AS
SELECT distinct stock1.belongsTo
FROM stock as stock1 inner join stock as stock2
ON stock1.belongsTo = stock2.belongsTo and stock1.stockDate < stock2.stockDate and stock1.stockValue>=stock2.stockValue;
CREATE VIEW improve_companies
AS
SELECT distinct symbol
FROM company
WHERE company.symbol NOT IN (SELECT not_improve_companies.belongsTo FROM not_improve_companies);
CREATE VIEW sector_with_glow
AS
SELECT company.companySector, count(company.symbol) as counter
FROM company INNER JOIN improve_companies
ON company.symbol = improve_companies.symbol
WHERE company.symbol = improve_companies.symbol
GROUP BY companySector
HAVING count(improve_companies.symbol) = 1;
CREATE VIEW companies_that_glow
AS
SELECT company.symbol, company.companySector
FROM company, sector_with_glow
WHERE company.companySector = sector_with_glow.companySector and company.symbol IN (
SELECT improve_companies.symbol
FROM improve_companies);
CREATE VIEW lastValue
AS
SELECT stock.belongsTo,MAX(stockDate) AS maxDate
FROM stock, companies_that_glow
WHERE stock.belongsTo = companies_that_glow.symbol
GROUP BY stock.belongsTo;
CREATE VIEW firstValue
AS
SELECT stock.belongsTo,MIN(stockDate) AS minDate
FROM stock, companies_that_glow
WHERE stock.belongsTo = companies_that_glow.symbol
GROUP BY stock.belongsTo;
CREATE VIEW firstPrice
AS
SELECT firstValue.belongsTo, stock.stockValue
FROM firstValue INNER JOIN stock on firstValue.belongsTo = stock.belongsTo and firstValue.minDate = stock.stockDate;
CREATE VIEW lastPrice
AS
SELECT lastValue.belongsTo, stock.stockValue
FROM lastValue INNER JOIN stock on lastValue.belongsTo = stock.belongsTo and lastValue.maxDate = stock.stockDate;
CREATE VIEW symbolVSyield
AS
SELECT firstPrice.belongsTo as Symbol, round(100*(lastPrice.stockValue - firstprice.stockValue)/firstprice.stockValue, 3) as Yield
FROM lastPrice, firstPrice
WHERE firstPrice.belongsTo=lastPrice.belongsTo;