Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.63 KB | None | 0 0
  1. -- (1)
  2. -- DIMStoreCustomer
  3. -- Selects store customers only
  4. -- Returns CustomerID in range [1, 701]
  5. SELECT c.customerID, c.PersonID, c.StoreID, s.Name, cr.Name AS CountryName,
  6.     cr.CountryRegionCode as CountryCode, sp.StateProvinceCode AS Region
  7. INTO DIMStoreCustomer
  8. FROM AdventureWorks2012.Sales.Customer AS c
  9.     JOIN AdventureWorks2012.Sales.Store AS s ON c.StoreID = s.BusinessEntityID
  10.     JOIN AdventureWorks2012.Person.BusinessEntityAddress AS ba ON s.BusinessEntityID = ba.BusinessEntityID
  11.     JOIN AdventureWorks2012.Person.Address AS a ON a.AddressID = ba.AddressID
  12.     JOIN AdventureWorks2012.Person.StateProvince AS sp ON a.StateProvinceID = sp.StateProvinceID
  13.     JOIN AdventureWorks2012.Person.CountryRegion AS cr ON sp.CountryRegionCode = cr.CountryRegionCode
  14. where c.PersonID IS NULL
  15.  
  16.  
  17.  
  18. -- (2)
  19. -- FactTable
  20. -- Returns CustomerID in range [11000, 30118]
  21. SELECT h.OrderDate, h.ShipDate, d.OrderQty as OrderQuantity, d.UnitPrice, d.UnitPriceDiscount, d.LineTotal,
  22.     h.CustomerID as IndividualCustomer, h.CustomerID as StoreCustomer, d.ProductID, h.SalesPersonID
  23. INTO FactTable
  24. FROM AdventureWorks2012.Sales.SalesOrderHeader AS h
  25.     JOIN AdventureWorks2012.Sales.SalesOrderDetail AS d ON d.SalesOrderID = h.SalesOrderID
  26.  
  27.  
  28.  
  29. -- (3)
  30. -- Filtering StoreCustomer in FactTable so that it contains StoreCustomer IDs only
  31. UPDATE FactTable
  32. SET FactTable.StoreCustomer = NULL
  33. WHERE StoreCustomer NOT IN (
  34.     SELECT DISTINCT f.StoreCustomer from FactTable f
  35.     JOIN AdventureWorks2012.Sales.Customer c on F.StoreCustomer = c.CustomerID
  36.     JOIN AdventureWorks2012.Sales.Store s on s.BusinessEntityID = c.StoreID)
  37. -- (60398 row(s) affected)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement