|
| 1 | +-- Which Supplier solding which products? |
| 2 | + |
| 3 | +SELECT Suppliers.SupplierID, Suppliers.CompanyName, Products.ProductID, Products.ProductName |
| 4 | +FROM Suppliers |
| 5 | +JOIN Products ON Suppliers.SupplierID = Products.SupplierID |
| 6 | +WHERE Suppliers.SupplierID = 1; |
| 7 | + |
| 8 | + |
| 9 | +-- Customers who did not ordered |
| 10 | + |
| 11 | +SELECT Orders.OrderID, Customers.ContactName |
| 12 | +FROM Customers |
| 13 | +LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID |
| 14 | +WHERE Orders.OrderID IS NULL; |
| 15 | + |
| 16 | + |
| 17 | +-- My sellings by products. |
| 18 | + |
| 19 | +SELECT P.ProductName as [Product Name], |
| 20 | + sum(O.Quantity) as Unit, |
| 21 | + sum(O.UnitPrice) as [Unit Price], |
| 22 | + sum(O.Discount) as Discount, |
| 23 | + sum(O.Quantity * (O.UnitPrice * (1 - O.Discount))) as Profit |
| 24 | +FROM Products as P |
| 25 | +JOIN [Order Details] as O ON O.ProductID = P.ProductID |
| 26 | +GROUP BY P.ProductName |
| 27 | +ORDER BY 1; |
| 28 | + |
| 29 | + |
| 30 | +-- My sellings by categories. |
| 31 | + |
| 32 | +SELECT C.CategoryName as [Category Name], |
| 33 | + sum(O.Quantity) as Unit, |
| 34 | + sum(O.UnitPrice) as [Unit Price], |
| 35 | + sum(O.Discount) as Discount, |
| 36 | + sum(O.Quantity * (O.UnitPrice * (1 - O.Discount))) as Profit |
| 37 | +FROM Categories as C |
| 38 | +JOIN Products as P ON P.CategoryID = C.CategoryID |
| 39 | +JOIN [Order Details] as O ON O.ProductID = P.ProductID |
| 40 | +GROUP BY C.CategoryName |
| 41 | +ORDER BY 1; |
| 42 | + |
| 43 | + |
| 44 | +-- Total freights by ship company. |
| 45 | + |
| 46 | +SELECT S.CompanyName as Shipper, |
| 47 | + sum(O.Freight) as Freight |
| 48 | +FROM Shippers as S |
| 49 | +JOIN Orders as O ON S.ShipperID = O.ShipVia |
| 50 | +GROUP BY S.CompanyName; |
| 51 | + |
| 52 | + |
| 53 | +-- Best customer |
| 54 | + |
| 55 | +SELECT TOP 1 C.CustomerID as Customer, |
| 56 | + sum(OD.Quantity * (OD.UnitPrice * (1 - OD.Discount))) as Total |
| 57 | +FROM Customers as C |
| 58 | +JOIN Orders as O ON C.CustomerID = O.CustomerID |
| 59 | +JOIN [Order Details] as OD ON O.OrderID = OD.OrderID |
| 60 | +GROUP BY C.CustomerID |
| 61 | +ORDER BY Total DESC; |
| 62 | + |
| 63 | + |
| 64 | +-- Total product selling by supplier company |
| 65 | + |
| 66 | +SELECT S.CompanyName as Supplier, |
| 67 | + P.ProductName as Product, |
| 68 | + sum(OD.Quantity) as [Total Quantity] |
| 69 | +FROM Suppliers as S |
| 70 | +JOIN Products as P ON S.SupplierID = P.SupplierID |
| 71 | +JOIN [Order Details] as OD ON P.ProductID = OD.ProductID |
| 72 | +GROUP BY S.CompanyName, P.ProductName |
| 73 | +ORDER BY 3 DESC; |
| 74 | + |
| 75 | +-- Order |
| 76 | +-- Customer |
| 77 | +-- Employee |
| 78 | +-- Date |
| 79 | +-- Shipper |
| 80 | +-- Price |
| 81 | +-- Product's category |
| 82 | +-- Product's supplier |
| 83 | + |
| 84 | +SELECT O.OrderID, |
| 85 | + C.CompanyName as Customer, |
| 86 | + E.FirstName+ ' '+ E.LastName as [Employee Name], |
| 87 | + O.OrderDate as [Date], |
| 88 | + S.CompanyName as [Shipper], |
| 89 | + OD.Quantity as Quantity, |
| 90 | + OD.UnitPrice as [Unit Price], |
| 91 | + OD.Discount as Discount, |
| 92 | + sum(OD.Quantity * (OD.UnitPrice * ( 1 - OD.Discount ))) as Total, |
| 93 | + Cat.CategoryName as Category, |
| 94 | + Sup.CompanyName as Supplier |
| 95 | +FROM Orders as O |
| 96 | + JOIN Customers as C ON O.CustomerID = C.CustomerID |
| 97 | + JOIN Employees as E ON O.EmployeeID = E.EmployeeID |
| 98 | + JOIN Shippers as S ON O.ShipVia = S.ShipperID |
| 99 | + JOIN [Order Details] as OD ON O.OrderID = OD.OrderID |
| 100 | + JOIN Products as P ON OD.ProductID = P.ProductID |
| 101 | + JOIN Categories as Cat ON P.CategoryID = Cat.CategoryID |
| 102 | + JOIN Suppliers as Sup ON P.SupplierID = Sup.SupplierID |
| 103 | +GROUP BY |
| 104 | + O.OrderID, |
| 105 | + C.CompanyName, |
| 106 | + E.FirstName, |
| 107 | + E.LastName, |
| 108 | + O.OrderDate, |
| 109 | + S.CompanyName, |
| 110 | + OD.Quantity, |
| 111 | + OD.UnitPrice, |
| 112 | + OD.Discount, |
| 113 | + Cat.CategoryName, |
| 114 | + Sup.CompanyName |
| 115 | +ORDER BY |
| 116 | + O.OrderID; |
0 commit comments