-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize_views.sql
96 lines (87 loc) · 3.12 KB
/
initialize_views.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
/*
* Kamil Sztandur 07.04.2022
* Use this script to recreate useful views for this database.
*/
USE bakeryapp
GO
IF OBJECT_ID('main.SoldProducts') IS NOT NULL
DROP VIEW main.SoldProducts
IF OBJECT_ID('main.EarningsPerProduct') IS NOT NULL
DROP VIEW main.EarningsPerProduct
IF OBJECT_ID('main.BakeriesEarnings') IS NOT NULL
DROP VIEW main.BakeriesEarnings
IF OBJECT_ID('main.PremiumClients') IS NOT NULL
DROP VIEW main.PremiumClients
IF OBJECT_ID('main.GetTotalMoneySpentByClient') IS NOT NULL
DROP FUNCTION main.GetTotalMoneySpentByClient
IF OBJECT_ID('main.ClientsExpenses') IS NOT NULL
DROP VIEW main.ClientsExpenses
GO
-- Sold products
CREATE VIEW main.SoldProducts AS
SELECT r.id as 'ReceiptID',
bakerycode AS 'BakeryCode',
(c.Surname + ' ' + c.Name) AS 'Customer',
p.Name, totalprice AS 'TotalPrice',
date AS 'Date'
FROM main.Receipts AS r, main.Products AS p, main.Clients as c
WHERE p.Id = r.ProductId AND c.Id = r.ClientId;
GO
-- Earnings per product
CREATE VIEW main.EarningsPerProduct AS
SELECT p.Id,
p.Name,
p.Price as 'PricePerUnit',
COUNT(r.TotalPrice) AS 'UnitsSold',
ISNULL(SUM(r.TotalPrice), 0) AS 'TotalEarnings'
FROM main.Products AS p
LEFT JOIN main.Receipts AS r ON p.Id = r.ProductId
GROUP BY p.Id, p.Name, p.Price;
GO
-- Earnings per bakery
CREATE VIEW main.BakeriesEarnings AS
SELECT b.BakeryCode AS 'BakeryCode' ,
(b.PostalCode + ' ' + b.TownName) AS 'PostalAddress',
(b.StreetName + ' ' + CAST(b.StreetNumber AS NVARCHAR(10))) AS 'StreetAddress',
COUNT(r.TotalPrice) AS 'UnitsSold',
ISNULL(SUM(r.TotalPrice), 0) AS 'TotalEarnings'
FROM main.BAKERIES as b
LEFT JOIN main.Receipts AS r ON b.BakeryCode = r.BakeryCode
GROUP BY b.BakeryCode,
(b.PostalCode + ' ' + b.TownName),
(b.StreetName + ' ' + CAST(b.StreetNumber AS NVARCHAR(10)));
GO
-- Premium clients showcase
CREATE FUNCTION main.GetTotalMoneySpentByClient(@clientId int)
RETURNS FLOAT AS
BEGIN
DECLARE @totalMoneySpent FLOAT
SET @totalMoneySpent = (SELECT SUM(TotalPrice) FROM main.Receipts WHERE ClientId = @clientId)
RETURN @totalMoneySpent
END
GO
CREATE VIEW main.PremiumClients AS
SELECT c.id AS 'ClientID',
(c.name + ' ' + c.surname) AS 'Client',
CAST(
IIF(
main.GetTotalMoneySpentByClient(c.id) > d.MoneyThreshold AND c.Id != 1,
1,
0
) AS BIT
) AS 'IsPremium'
FROM main.Clients AS c, main.Discounts AS d
WHERE d.Description = 'Premium client';
GO
-- Expenses per clients
CREATE VIEW main.ClientsExpenses AS
SELECT c.id AS 'ClientID',
(c.name + ' ' + c.surname) AS 'Client',
main.GetTotalMoneySpentByClient(c.id) AS 'TotalExpenses',
COUNT(r.Id) AS 'TotalUnitsBought'
FROM main.Clients AS c, main.Receipts AS r
WHERE r.ClientId = c.Id
GROUP BY c.id,
(c.name + ' ' + c.surname),
main.GetTotalMoneySpentByClient(c.id);
GO