-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.3 Views
More file actions
55 lines (47 loc) · 1.05 KB
/
2.3 Views
File metadata and controls
55 lines (47 loc) · 1.05 KB
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
--view showing all tables
CREATE VIEW vw_PlantsOrdersCustomers AS
SELECT
C.CustomerID,
C.CustomerName,
P.PlantID,
P.Name AS PlantName,
P.OriginCountry,
O.OrderID,
O.OrderDate
FROM
Customers C
JOIN Orders O ON C.CustomerID = O.CustomerID
JOIN Plants P ON O.PlantID = P.PlantID;
SELECT *
FROM vw_PlantsOrdersCustomers;
-- view showing customer addresses
CREATE VIEW vw_CustomerAddresses AS
SELECT
C.CustomerID,
C.CustomerName,
C.Address,
C.Country
FROM
Customers C
JOIN Orders O ON C.CustomerID = O.CustomerID
GROUP BY
C.CustomerID,
C.CustomerName,
C.Address,
C.Country;
SELECT * FROM vw_CustomerAddresses;
--view that retrives information about plants along with total number of orders
CREATE VIEW vw_PlantsWithTotalOrders AS
SELECT
P.PlantID,
P.Name AS PlantName,
P.OriginCountry,
COUNT(O.OrderID) AS TotalOrders
FROM
Plants P
LEFT JOIN Orders O ON P.PlantID = O.PlantID
GROUP BY
P.PlantID,
P.Name,
P.OriginCountry;
SELECT * FROM vw_PlantsWithTotalOrders;