Created
December 20, 2018 02:59
-
-
Save khairulfathi/76b79f697ae0eb291e46c2fa0c3f9b4e to your computer and use it in GitHub Desktop.
To Optimize `orders_summary_view.sql`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE VIEW `order_summary_opt` AS | |
SELECT | |
oo.OrderID as OrderID, | |
oo.CustomerID as CustomerID, | |
oo.EmployeeID as EmployeeID, | |
oo.OrderDate as OrderDate, | |
oo.RequiredDate as RequiredDate, | |
oo.ShippedDate as ShippedDate, | |
oo.ShipVia as ShipVia, | |
oo.ShipName as ShipName, | |
oo.ShipAddress as ShipAddress, | |
oo.ShipCity as ShipCity, | |
oo.ShipRegion as ShipRegion, | |
oo.ShipPostalCode as ShipPostalCode, | |
oo.ShipCountry as ShipCountry, | |
oo.Freight as Shipping, | |
COUNT(*) as TotalItems, | |
SUM(od.UnitPrice * od.Quantity) as TotalAmount, | |
SUM(od.UnitPrice * od.Quantity * od.Discount) as TotalDiscount, | |
SUM(od.UnitPrice * od.Quantity * (1 - od.Discount)) as DiscountedAmount, | |
SUM(od.UnitPrice * od.Quantity * (1 - od.Discount)) + oo.Freight as GrandTotal | |
FROM orders oo | |
JOIN order_details od | |
ON oo.OrderID = od.OrderID | |
WHERE 1 = 1 | |
GROUP BY OrderID, CustomerID, EmployeeID, OrderDate, | |
RequiredDate, ShippedDate, ShipVia, ShipName, ShipAddress, ShipCity, | |
ShipRegion, ShipPostalCode, ShipCountry, Shipping | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment