-
Notifications
You must be signed in to change notification settings - Fork 0
/
VIEW.sql
106 lines (99 loc) · 4.22 KB
/
VIEW.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
CREATE VIEW dbo.Invoicing
AS
SELECT DATEPART(YEAR, CreatedAt) AS year,
DATEPART(MONTH, CreatedAt) AS month,
SUM(Quantity) AS Quantity,
SUM(Quantity * Price) as AmountMade
FROM Sale
INNER JOIN SaleLine AS Line on Sale.SaleID = Line.SaleID
INNER JOIN ProductLine AS P on P.ProductLineID = Line.ProductLineID
GROUP BY DATEPART(YEAR, CreatedAt), DATEPART(MONTH, CreatedAt)
CREATE VIEW InvoicingByDay
AS
SELECT DATEPART(YEAR, CreatedAt) AS year,
DATEPART(MONTH, CreatedAt) AS month,
DATEPART(DAY, CreatedAt) AS day,
SUM(Quantity) AS Quantity,
SUM(Quantity * Price) as AmountMade
FROM Sale
INNER JOIN SaleLine AS Line on Sale.SaleID = Line.SaleID
INNER JOIN ProductLine AS P on P.ProductLineID = Line.ProductLineID
GROUP BY DATEPART(YEAR, CreatedAt), DATEPART(MONTH, CreatedAt), DATEPART(DAY, CreatedAt)
CREATE VIEW InvoicingByWeek
AS
SELECT DATEPART(YEAR, CreatedAt) AS year,
DATEPART(WEEK, CreatedAt) AS week,
SUM(Quantity) AS Quantity,
SUM(Quantity * Price) as AmountMade
FROM Sale
INNER JOIN SaleLine AS Line on Sale.SaleID = Line.SaleID
INNER JOIN ProductLine AS P on P.ProductLineID = Line.ProductLineID
GROUP BY DATEPART(YEAR, CreatedAt), DATEPART(WEEK, CreatedAt)
CREATE VIEW SalesByDay
AS
SELECT Product.Title,
ProductLine.Color,
ProductLine.Size,
Sale.CreatedAt,
SUM(SaleLine.Quantity) as Quantity
FROM Sale
INNER JOIN SaleLine on Sale.SaleID = SaleLine.SaleID
INNER JOIN ProductLine on ProductLine.ProductLineID = SaleLine.ProductLineID
INNER JOIN Product on Product.ProductID = ProductLine.ProductID
GROUP BY Sale.CreatedAt, ProductLine.Size, ProductLine.Color, Product.Title
CREATE VIEW RevenueBySeller
AS
SELECT Seller.SellerID,
CONCAT(Seller.FirstName, ' ', Seller.LastName) AS SellerName,
MONTH(Sale.CreatedAt) as Month,
YEAR(Sale.CreatedAt) as Year,
SUM(SaleLine.Quantity) AS Quantity,
SUM(SaleLine.Quantity * ProductLine.Price) as Revenue
FROM Seller
INNER JOIN Sale on Sale.SellerID = Seller.SellerID
INNER JOIN SaleLine on Sale.SaleID = SaleLine.SaleID
INNER JOIN ProductLine on ProductLine.ProductLineID = SaleLine.ProductLineID
GROUP BY Seller.SellerID, Sale.CreatedAt, Seller.FirstName, Seller.LastName
ORDER BY Sale.CreatedAt, Revenue DESC
OFFSET 0 ROWS
CREATE VIEW TopProducts
AS
SELECT ProductLine.ProductID,
Product.Title,
ProductLine.Size,
ProductLine.Color,
ProductLine.Price,
SUM(SL.Quantity) AS Quantity
FROM ProductLine
INNER JOIN Product on Product.ProductID = ProductLine.ProductID
INNER JOIN SaleLine SL on ProductLine.ProductLineID = SL.ProductLineID
GROUP BY ProductLine.ProductID, ProductLine.Price, ProductLine.Color, ProductLine.Size, Product.Title
ORDER BY Quantity DESC
OFFSET 0 ROWS
CREATE VIEW DefectView
AS
SELECT DATEPART(YEAR, Defect.CreatedAt) AS Year,
DATEPART(WEEK, Defect.CreatedAt) AS Week,
Product.Title,
Product.Description,
SUM(ProductLine.DefectQuantity) AS Quantity,
SUM(ProductLine.DefectQuantity * ProductLine.Price) AS Amount
FROM Defect
INNER JOIN ProductLine ON ProductLine.ProductLineID = Defect.ProductLineID
INNER JOIN Product ON Product.ProductID = ProductLine.ProductID
GROUP BY DATEPART(YEAR, Defect.CreatedAt) ,DATEPART(WEEK, Defect.CreatedAt), Product.Title, Product.Description;
CREATE VIEW Invoice
AS
SELECT Customer.NIF,
Sale.SaleID,
Product.Title,
ProductLine.Color,
ProductLine.Size,
SUM(SaleLine.Quantity) as Quantity,
SUM(SaleLine.Quantity * ProductLine.Price) as Amount
FROM Customer
INNER JOIN Sale ON Sale.CustomerID = Customer.CustomerID
INNER JOIN SaleLine ON SaleLine.SaleID = Sale.SaleID
INNER JOIN ProductLine ON ProductLine.ProductLineID = SaleLine.ProductLineID
INNER JOIN Product ON Product.ProductID = ProductLine.ProductID
GROUP by Customer.NIF, Sale.SaleID, Product.Title, ProductLine.Color, ProductLine.Size