-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainView.sql
61 lines (53 loc) · 2.23 KB
/
MainView.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
-- All recommendations that where written by a hiker that
--wrote recommendations on all parts of the trek
CREATE VIEW CompleteRecommendation
AS
SELECT *
FROM RecommendationsToHikes RTH
WHERE NOT EXISTS(
/* All countries that the trek(of a specific recommendation)
passes through */
(SELECT DISTINCT TPTC.Country
FROM TreksPassingThroughCountry TPTC
WHERE TPTC.Trek = RTH.Trek)
EXCEPT -- minus alternative
/* All countries (relevant to the trek in the recommendation)
that the specific hiker published recommendations on */
(SELECT DISTINCT RTH2.Country
FROM RecommendationsToHikes RTH2
WHERE RTH.WrittenBy = RTH2.WrittenBy AND
RTH.Trek = RTH2.Trek)
);
/* Finds all TrendLeaders as defined */
CREATE VIEW TrendLeaders
AS
SELECT H.ID, H.Name
FROM Hikers H -- Iterate over all Hikers
/* If a hiker has at least one trek that more than a 100 people follow
him after his Complete recommendation then he becomes a TrendLeader*/
WHERE EXISTS(SELECT *
FROM Treks -- For each Hiker Iterate over all Treks
WHERE 100 < (SELECT COUNT(DISTINCT Hikes.Hiker)
FROM Hikes,
/* Returns the last recommendation by the person, about the trek */
(SELECT TOP 1 CR.Trek, CR.TimeStamp
FROM CompleteRecommendation CR
WHERE H.ID = CR.WrittenBy AND Treks.Name = CR.Trek
ORDER BY CR.TimeStamp DESC) TheTrek
/* Verify no more than a year has passed since recommendation*/
WHERE Hikes.trek = TheTrek.Trek AND
DATEDIFF(Day, TheTrek.TimeStamp, Hikes.Date) < 365)
);
-- Return Stats as requested
SELECT NumOfTreks.ID, Name, NumberOfTreks, NumberOfRecommendations
FROM
(SELECT TL.ID, TL.Name, COUNT(*) AS NumberOfTreks
FROM Hikes H, TrendLeaders TL
WHERE H.Hiker = TL.ID
GROUP BY TL.ID, TL.Name) NumOfTreks,
(SELECT TL.ID, COUNT(*) AS NumberOfRecommendations
FROM Recommendations R, TrendLeaders TL
WHERE R.WrittenBy = TL.ID
GROUP BY TL.ID) NumOfRecommendations
WHERE NumOfTreks.ID = NumOfRecommendations.ID
ORDER BY NumOfTreks.ID ASC;