elena1234

Calendar Table ( T-SQL )

Mar 15th, 2022 (edited)
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.13 KB | None | 0 0
  1. USE AdventureWorks2019
  2. GO
  3.  
  4.  
  5. -- Exercise 1
  6. -- Update your calendar lookup table with a few holidays of your choice that always fall on the same day of the year - for example,
  7. -- New Year's.
  8. UPDATE AdventureWorks2019.dbo.Calendar
  9. SET
  10. HolidayFlag =
  11.     CASE
  12.         WHEN DayOfMonthNumber = 31 AND MonthNumber = 12 THEN 1
  13.         WHEN DayOfMonthNumber = 3 AND MonthNumber = 3 THEN 1
  14.         WHEN DayOfMonthNumber = 24 AND MonthNumber = 5 THEN 1
  15.         ELSE 0
  16.     END
  17.  
  18. SELECT * FROM AdventureWorks2019.dbo.Calendar
  19.  
  20.  
  21. -- Exercise 2
  22. -- Using your updated calendar table, pull all purchasing orders that were made on a holiday.
  23. -- It's fine to simply select all columns via SELECT *.
  24. SELECT *
  25. FROM [Purchasing].[PurchaseOrderHeader] A
  26. JOIN AdventureWorks2019.dbo.Calendar B
  27. ON A.OrderDate = B.DateValue
  28. WHERE B.HolidayFlag = 1
  29.  
  30.  
  31. -- Exercise 3
  32. -- Again using your updated calendar table, now pull all purchasing orders that were made on a holiday that also fell on a weekend.
  33. SELECT *
  34. FROM [Purchasing].[PurchaseOrderHeader] A
  35. JOIN AdventureWorks2019.dbo.Calendar B
  36. ON A.OrderDate = B.DateValue
  37. WHERE B.WeekendFlag = 1 AND B.HolidayFlag = 1
  38.  
Add Comment
Please, Sign In to add comment