-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputed-columns.sql
150 lines (134 loc) · 3.4 KB
/
computed-columns.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
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
USE Proj_A10
GO
----------------------
-- COMPUTED COLUMNS --
----------------------
--NumOfGamesBought (Gamer Table) � must be unique
-- Creator: Marcus Huang --
CREATE FUNCTION numGamesBought(@GamerID INT)
RETURNS INT
AS
BEGIN
DECLARE @RET INT = (SELECT COUNT(DISTINCT OG.GameID)
FROM tblORDER_GAME OG
JOIN tblORDER O ON OG.OrderID = O.OrderID
JOIN tblGAMER G ON O.GamerID = G.GamerID
WHERE O.GamerID = @GamerID)
RETURN @RET
END
GO
ALTER TABLE tblGAMER
ADD NumBought
AS dbo.numGamesBought(GamerID)
GO
-- HistoryCurrent for tblPlatform_Price_History
-- Creator: Elisa Truong --
CREATE FUNCTION isPriceCurrent(@Start DATE, @End DATE)
RETURNS BIT
AS
BEGIN
DECLARE @RET BIT = 0
DECLARE @CurrentDate DATE = (SELECT GETDATE())
IF (@End IS NULL OR @CurrentDate BETWEEN @START AND @END)
BEGIN
SET @RET = 1
END
RETURN @RET
END
GO
ALTER TABLE tblPlatform_Price_History
ADD HistoryCurrent AS dbo.isPriceCurrent(HistoryStart, HistoryEnd)
GO
--GameCurrentPrice (GamePlatform table)
-- Creator: Marcus Huang --
CREATE FUNCTION gameCurrentPrice(@GamePlatformID INT)
RETURNS INT
AS
BEGIN
DECLARE @RET INT = (SELECT PPH.HistoryPrice
FROM tblGamePlatform GP
JOIN tblPlatform_Price_History PPH ON GP.GamePlatformID = PPH.GamePlatformID
WHERE HistoryCurrent = 1 AND GP.GamePlatformID = @GamePlatformID
)
RETURN @RET
END
GO
ALTER TABLE tblGamePlatform
ADD CurrentPrice
AS dbo.gameCurrentPrice(GamePlatformID)
GO
-- PriceRange for tblGAME
-- Creator: Elisa Truong --
CREATE FUNCTION PriceRange(@GameID INT)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @MAX MONEY = (SELECT TOP 1 CurrentPrice FROM tblGamePlatform WHERE GameID = @GameID ORDER BY CurrentPrice DESC)
DECLARE @MIN MONEY = (SELECT TOP 1 CurrentPrice FROM tblGamePlatform WHERE GameID = @GameID ORDER BY CurrentPrice ASC)
DECLARE @RET VARCHAR(50)
IF @MAX IS NULL AND @MIN IS NULL
RETURN NULL
SET @RET = (SELECT CONCAT(@MAX, ' - ', @MIN))
RETURN @RET
END
GO
ALTER TABLE tblGAME
ADD PriceRange AS dbo.PriceRange(GameID)
GO
-- Computed Column: Age of Gamer (Gamer Table) --
-- Creator: Angel Lin --
ALTER TABLE tblGAMER
ADD GamerAge
AS (CONVERT(INT,CONVERT(CHAR(8),GETDATE(),112))-CONVERT(CHAR(8),GamerDOB,112))/10000
GO
-- Computed Column: Most Recently Bought Game (Gamer Table) --
-- Creator: Angel Lin --
CREATE FUNCTION recentBoughtGame(@PK INT)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @RET VARCHAR(50) =
(SELECT TOP 1 G.GameName
FROM tblGame AS G
JOIN tblOrder_Game AS OG ON G.GameID = OG.GameID
JOIN tblOrder AS O ON OG.OrderID = O.OrderID
JOIN tblGamer AS GR ON O.GamerID = GR.GamerID
WHERE GR.GamerID = @PK
ORDER BY O.OrderDate DESC)
RETURN @RET
END
GO
ALTER TABLE tblGAMER
ADD recentBoughtGame AS dbo.recentBoughtGame(GamerID)
GO
-- Creator: Andi Ren --
CREATE FUNCTION AverageReview(@GameID INT)
RETURNS FLOAT
AS
BEGIN
RETURN (SELECT AVG(ReviewRating)
FROM tblGAME G
JOIN tblORDER_GAME OG ON G.GameID = OG.GameID
JOIN tblREVIEW R ON R.OrderGameID = OG.OrderGameID
WHERE G.GameID = @GameID
)
END
GO
ALTER TABLE tblGAME
ADD AvgReview AS dbo.AverageReview(GameID)
GO
-- Creator: Andi Ren --
CREATE FUNCTION LanguageNum(@GameID INT)
RETURNS INT
AS
BEGIN
RETURN (SELECT COUNT(GameLanguageID)
FROM tblGAME G
JOIN tblGAME_LANGUAGE GL ON G.GameID = GL.GameID
WHERE G.GameID = @GameID
)
END
GO
ALTER TABLE tblGAME
ADD GameNumLanguagesSupport AS dbo.LanguageNum(GameID)
GO