luisnaranjo733

Untitled

Oct 14th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.07 KB | None | 0 0
  1. /* Which employees have sold the highest & lowest number of bikes to customers
  2. in Washington State and Utah before March 9, 2008? */
  3.  
  4. --Pe.FirstName, SOD.OrderQty, StateP.StateProvinceCode
  5.  
  6. SELECT Pe.FirstName, MAX(SOD.OrderQty) AS max_order FROM [Person].[CountryRegion] CR
  7. JOIN [Person].[StateProvince] StateP ON CR.CountryRegionCode = StateP.CountryRegionCode
  8. JOIN [Sales].[SalesTerritory] ST ON StateP.TerritoryID = ST.TerritoryID
  9. JOIN [Sales].[SalesOrderHeader] SOH ON ST.TerritoryID = SOH.TerritoryID
  10. JOIN [Sales].[SalesOrderDetail] SOD ON SOH.SalesOrderID = SOD.SalesOrderID
  11. JOIN [Sales].[SpecialOfferProduct] SOP ON SOD.ProductID = SOP.ProductID
  12. JOIN [Production].[Product] Pr ON SOP.ProductID = Pr.ProductID
  13. JOIN [Production].[ProductCategory] PC ON Pr.ProductSubcategoryID = PC.ProductCategoryID
  14. JOIN [Sales].[SalesPerson] SalesP ON ST.TerritoryID = SalesP.TerritoryID
  15. JOIN [Person].[Person] Pe ON SalesP.BusinessEntityID = PE.BusinessEntityID
  16. WHERE PC.Name LIKE 'Bike%' AND StateP.StateProvinceCode IN ('WA', 'UT') AND SOH.ShipDate < '3/9/2008'
  17. GROUP BY Pe.FirstName
Advertisement
Add Comment
Please, Sign In to add comment