Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. SELECT sod.SalesOrderID AS TransactionID,
  2. SalesOrderDetailID AS TransactionDetailID,
  3. convert(date,DateID,23) AS DateID,
  4. soh.TerritoryID AS LocationID,
  5. ProductID,
  6. soh.CustomerID,
  7. UnitPrice,
  8. OrderQty,
  9. LineTotal,
  10. UnitPriceDiscount,
  11. (UnitPriceDiscount*UnitPrice*OrderQty) AS TotalDiscount
  12. INTO dwaw.CustomerSales
  13. FROM AdventureWorks2017.Sales.SalesOrderDetail sod
  14. INNER JOIN AdventureWorks2017.Sales.SalesOrderHeader soh
  15. ON sod.SalesOrderID = soh.SalesOrderID
  16. INNER JOIN dwaw.dimDate
  17. ON dwaw.dimDate.day = DAY(soh.OrderDate)
  18. AND dwaw.dimDate.month = MONTH(soh.OrderDate)
  19. AND dwaw.dimDate.year = YEAR(soh.OrderDate)
  20. INNER JOIN AdventureWorks2017.Sales.Customer
  21. ON soh.CustomerID = AdventureWorks2017.Sales.Customer.CustomerID
  22. INNER JOIN AdventureWorks2017.Person.Person
  23. ON AdventureWorks2017.Sales.Customer.PersonID = AdventureWorks2017.Person.Person.BusinessEntityID
  24. WHERE PersonType = 'IN'
  25.  
  26.  
  27. ALTER TABLE dwaw.CustomerSales
  28. ADD PRIMARY KEY(TransactionDetailID);
  29.  
  30. ALTER TABLE dwaw.CustomerSales
  31. ALTER COLUMN TransactionDetailID
  32. INTEGER NOT NULL;
  33.  
  34. ALTER TABLE dwaw.CustomerSales
  35. ALTER COLUMN TransactionID
  36. INTEGER NOT NULL;
  37.  
  38. ALTER TABLE dwaw.CustomerSales
  39. ALTER COLUMN DateID DATE NOT NULL;
  40.  
  41. ALTER TABLE dwaw.CustomerSales
  42. ALTER COLUMN CustomerID
  43. INTEGER NOT NULL;
  44.  
  45. ALTER TABLE dwaw.CustomerSales
  46. ALTER COLUMN LocationID
  47. INTEGER NOT NULL;
  48.  
  49. ALTER TABLE dwaw.CustomerSales
  50. ALTER COLUMN ProductID
  51. INTEGER NOT NULL;
  52.  
  53. ALTER TABLE dwaw.CustomerSales
  54. ADD FOREIGN KEY(DateID)
  55. REFERENCES dwaw.dimDate([date]);
  56.  
  57. ALTER TABLE dwaw.CustomerSales
  58. ADD FOREIGN KEY (CustomerID)
  59. REFERENCES dwaw.DIMCustomer(CustomerID);
  60.  
  61. ALTER TABLE dwaw.CustomerSales
  62. ADD FOREIGN KEY (LocationID)
  63. REFERENCES dwaw.DIMSalesLocation(LocationID);
  64.  
  65. ALTER TABLE dwaw.CustomerSales
  66. ADD FOREIGN KEY (ProductID)
  67. REFERENCES DWAW.DIMProduct(ProductID);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement