-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.sql
274 lines (244 loc) · 9.83 KB
/
functions.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
-- Wypisz zamówienie po id
CREATE FUNCTION GetDetailsOfOrder(@input int)
RETURNS table AS
RETURN
SELECT OrderDetails.Quantity*ProductPrices.UnitPrice as Price, OrderDetails.Quantity*ProductPrices.UnitPrice*(1-Orders.DiscountPercent/100.0) as PriceAfterDiscount,
Orders.OrderID,Orders.OrderDate, Orders.OrderStatus, Orders.PayVia
FROM Orders
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE Orders.OrderID = @input
go
-- sprawdzenie stanu zamówienia po id
CREATE FUNCTION GetStateOfOrder(@input int)
RETURNS table AS
RETURN
SELECT Orders.OrderStatus
FROM Orders
WHERE Orders.OrderID = @input
go
-- wyposanie danych pracownika po id
CREATE FUNCTION GetDataOfEmployeee(@input int)
RETURNS table AS
RETURN
SELECT RestaurantEmployees.FirstName as Imie, RestaurantEmployees.LastName as Nazwisko, RestaurantEmployees.Phone as DaneKontaktowe, RestaurantEmployees.Occupation as Stanowisko, EmployeesSalary.FromTime as ZatrudnionyOd, EmployeesSalary.ToTime as ZatrudnionyDo, EmployeesSalary.Salary
FROM RestaurantEmployees
INNER JOIN EmployeesSalary ON EmployeesSalary.RestaurantEmployeeID = RestaurantEmployees.RestaurantEmployeeID
WHERE RestaurantEmployees.RestaurantEmployeeID = @input
go
-- zamówienia powyżej jakiejś wartości
CREATE FUNCTION GetOrdersAboveValue(@input int)
RETURNS table AS
RETURN
SELECT Orders.OrderID, Orders.OrderStatus, OrderDetails.Quantity*ProductPrices.UnitPrice*(1-Orders.DiscountPercent/100.0) AS PriceAfterDiscount
FROM Orders
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE OrderDetails.Quantity*ProductPrices.UnitPrice > @input
go
-- suma zamówień danego dnia
CREATE FUNCTION GetValueOfOrdersOnDay(@date date)
RETURNS table AS
RETURN
SELECT SUM(OrderDetails.Quantity*ProductPrices.UnitPrice*(1-Orders.DiscountPercent/100.0)) as Suma
FROM Orders
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE YEAR(@date) = YEAR(Orders.OrderDate)
AND MONTH(@date) = MONTH(Orders.OrderDate)
AND DAY(@date) = DAY(Orders.OrderDate)
go
-- suma zamówień danego miesiąca
CREATE FUNCTION GetValueOfOrdersOnMonth(@date date)
RETURNS table AS
RETURN
SELECT SUM(OrderDetails.Quantity*ProductPrices.UnitPrice*(1-Orders.DiscountPercent/100.0)) as Suma
FROM Orders
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE YEAR(@date) = YEAR(Orders.OrderDate)
AND MONTH(@date) = MONTH(Orders.OrderDate)
go
-- wartosc X zamówienia
CREATE FUNCTION GetValueOfOrder(@input int)
RETURNS int AS
BEGIN
DECLARE @value INT;
SELECT @value = OrderDetails.Quantity*ProductPrices.UnitPrice*(1-Orders.DiscountPercent/100.0)
FROM Orders
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE (Orders.OrderID = @input) AND (Orders.OrderDate >= ProductPrices.FromTime AND ( ProductPrices.ToTime is NULL OR ProductPrices.ToTime>=Orders.OrderDate))
RETURN IsNull(@value, 0);
END
go
-- wartosc najtańszy produkt w kategorii
CREATE FUNCTION GetCheapestProductInCategory(@input int)
RETURNS table AS
RETURN
SELECT TOP(1) ProductPrices.UnitPrice as cena, ProductName
FROM Products
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID
WHERE Categories.CategoryID = @input
ORDER BY cena DESC
go
-- wartosc najdroższy produkt w kategorii
CREATE FUNCTION GetMostExpensiveProductInCategory(@input char)
RETURNS table AS
RETURN
SELECT TOP(1) ProductPrices.UnitPrice as cena, ProductName
FROM Products
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID
WHERE Categories.CategoryID = @input
go
-- GetIngredientsForProduct
CREATE FUNCTION GetIngredientsForProduct(@ProductID int)
RETURNS table
AS
RETURN
SELECT IngredientName
FROM ProductIngredients
JOIN IngredientsWarehouse ON IngredientsWarehouse.IngredientID = ProductIngredients.IngredientID
WHERE ProductIngredients.ProductID = @ProductID
GO
-- GetProductsFromCategory
CREATE FUNCTION GetProductsFromCategory(@CategoryID int)
RETURNS table
AS
RETURN
SELECT Products.ProductName
FROM Products
JOIN Categories ON Categories.CategoryID = Products.CategoryID
WHERE Categories.CategoryID = @CategoryID
GO
-- GetProductsFromMenu
CREATE FUNCTION GetProductsFromMenu(@MenuID int)
RETURNS table
AS
RETURN
SELECT Products.ProductName
FROM Menu
JOIN MenuDetails ON MenuDetails.MenuID = Menu.MenuID
JOIN Products ON Products.ProductID = MenuDetails.ProductID
WHERE Menu.MenuID = @MenuID
GO
-- GetCurrentEmployeeSalary
CREATE FUNCTION GetCurrentEmployeeSalary(@EmployeeID int)
RETURNS table
AS
RETURN
SELECT Salary
FROM EmployeesSalary
WHERE RestaurantEmployeeID = @EmployeeID
AND ToTime IS NULL
GO
-- GetCurrentAverageSalaryForOccupation
CREATE FUNCTION GetCurrentAverageSalaryForOccupation(@Occupation varchar(64))
RETURNS table
AS
RETURN
SELECT AVG(Salary) AS AveragePriceForOccupation
FROM EmployeesSalary
JOIN RestaurantEmployees ON EmployeesSalary.RestaurantEmployeeID = RestaurantEmployees.RestaurantEmployeeID
WHERE Occupation = @Occupation
AND ToTime IS NULL
GO
-- GetTotalProductsAndAveragePriceOfMenu
CREATE FUNCTION GetTotalProductsAndAveragePriceOfMenu(@MenuID int)
RETURNS table
AS
RETURN
SELECT COUNT(UnitPrice) AS TotalProducts, AVG(UnitPrice) AS AverageUnitPrice
FROM Menu
JOIN MenuDetails ON Menu.MenuID = MenuDetails.MenuID
JOIN ProductPrices ON MenuDetails.ProductID = ProductPrices.ProductID
WHERE Menu.MenuID = @MenuID
AND ProductPrices.ToTime IS NULL
GO
-- GetHighestSalaryForEmployee
CREATE FUNCTION GetHighestSalaryForEmployee(@EmployeeID int)
RETURNS table
AS
RETURN
SELECT MAX(Salary) AS MaxEmployeeSalary
FROM EmployeesSalary
WHERE RestaurantEmployeeID = @EmployeeID
GO
--Sprawdzenie ile dni pozostało na dane menu - pracownik
CREATE FUNCTION RemainingDaysForMenu(@MenuID int)
RETURNS table
AS
RETURN
SELECT DATEDIFF(day, GETDATE(), (SELECT
ToTime
FROM Menu
WHERE MenuID = @MenuID )) as [remaining days]
GO
CREATE FUNCTION RemainingFreeSeats()
RETURNS table
AS
RETURN
SELECT (
(SELECT
SUM(NumberOfSeats)
FROM DiningTables) -
ISNULL((SELECT
SUM(Seats)
FROM Reservation
WHERE GETDATE() >= FromTime AND GETDATE() <= ToTime
),0)
) as [free seats]
GO
CREATE FUNCTION CanAccommodateCustomers(@customers int)
RETURNS table
AS
RETURN
SELECT (CASE WHEN free_seats.nrOfFreeSeats >= @customers THEN 'true' ELSE 'false' END) AS freeSpaces
FROM (SELECT ((SELECT SUM(NumberOfSeats) FROM DiningTables) - (SELECT SUM(Seats) FROM Reservation WHERE GETDATE() >= FromTime AND GETDATE() <= ToTime)) as nrOfFreeSeats
) as free_seats
GO
CREATE FUNCTION invoice(@ordersID int)
RETURNS table
AS
RETURN
SELECT Products.ProductName as[nazwa produktu],
OrderDetails.Quantity as [ilosc],
ProductPrices.UnitPrice as [cena produktu],
Orders.DiscountPercent as [zniżka],
ProductPrices.UnitPrice*(1- (Orders.DiscountPercent/100.0)) as [cena produktu z uwzględnieniem zniżki],
Orders.OrderDate as [data zamowienia] FROM Orders
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON Products.ProductID = ProductPrices.ProductID
WHERE ProductPrices.FromTime <= Orders.OrderDate AND (ProductPrices.ToTime IS NULL OR ProductPrices.ToTime >= Orders.OrderDate)
AND Orders.OrderID = @ordersID
GO
ALTER FUNCTION collectiveInvoice(@companyID int)
RETURNS table
AS
RETURN
SELECT
SUM(OrderDetails.Quantity*ProductPrices.UnitPrice*(1-(Orders.DiscountPercent/100.0))) as [calkowita cena produktow],
ROUND(AVG(Orders.DiscountPercent),2) as [srednia znizek],
Orders.OrderDate as [data zamowienia],
Companies.NIP as [NIP firmy]
FROM Companies
INNER JOIN Customers ON Companies.CustomerID = Customers.CustomerID
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON Products.ProductID = ProductPrices.ProductID
WHERE
MONTH(Orders.OrderDate) = MONTH(GETDATE())
AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
AND Companies.CompanyID = @companyID
AND ProductPrices.FromTime <= Orders.OrderDate AND (ProductPrices.ToTime IS NULL OR ProductPrices.ToTime >= Orders.OrderDate)
GROUP BY Orders.OrderDate,Companies.NIP
GO