Advertisement
elena1234

Optimizing With UPDATE ( T-SQL )

Mar 14th, 2022 (edited)
1,963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.28 KB | None | 0 0
  1. USE AdventureWorks2019
  2. GO
  3.  
  4. --Exercise
  5. -- Making use of temp tables and UPDATE statements, re-write an optimized version of the query in the
  6. -- "Optimizing With UPDATE - Exercise Starter Code.sql" file, which you'll find in the resources for this section.
  7. SELECT
  8.        A.BusinessEntityID
  9.       ,A.Title
  10.       ,A.FirstName
  11.       ,A.MiddleName
  12.       ,A.LastName
  13.       ,B.PhoneNumber
  14.       ,PhoneNumberType = C.Name
  15.       ,D.EmailAddress
  16.  
  17. FROM AdventureWorks2019.Person.Person A
  18.     LEFT JOIN AdventureWorks2019.Person.PersonPhone B
  19.         ON A.BusinessEntityID = B.BusinessEntityID -- BusinessEntityID
  20.     LEFT JOIN AdventureWorks2019.Person.PhoneNumberType C
  21.         ON B.PhoneNumberTypeID = C.PhoneNumberTypeID -- PhoneNumberTypeID  
  22.     LEFT JOIN AdventureWorks2019.Person.EmailAddress D
  23.         ON A.BusinessEntityID = D.BusinessEntityID
  24.  
  25.  
  26. -- Your Code
  27. -- a) Create Core Temp Table:
  28. CREATE TABLE #Names
  29. (
  30. BusinessEntityID int,
  31. Title nvarchar(8),
  32. FirstName nvarchar(50),
  33. MiddleName nvarchar(50),
  34. LastName nvarchar(50)
  35. )
  36. INSERT INTO #Names
  37. (
  38. BusinessEntityID,
  39. Title,
  40. FirstName,
  41. MiddleName,
  42. LastName
  43. )
  44. SELECT
  45. BusinessEntityID,
  46. Title,
  47. FirstName,
  48. MiddleName,
  49. LastName
  50. FROM AdventureWorks2019.Person.Person
  51.  
  52. -- b) Create Master Total Temp Table:
  53. CREATE TABLE #Master
  54. (
  55. BusinessEntityID int,
  56. Title nvarchar(8),
  57. FirstName nvarchar(50),
  58. MiddleName nvarchar(50),
  59. LastName nvarchar(50),
  60. PhoneNumber nvarchar(25),
  61. PhoneNumberTypeID int,
  62. PhoneNumberType nvarchar(50),
  63. EmailAddress nvarchar(50)
  64. )
  65. INSERT INTO #Master
  66. (
  67. BusinessEntityID,
  68. Title,
  69. FirstName,
  70. MiddleName,
  71. LastName,
  72. PhoneNumber,
  73. PhoneNumberTypeID
  74. )
  75. SELECT
  76. A.BusinessEntityID,
  77. A.Title,
  78. A.FirstName,
  79. A.MiddleName,
  80. A.LastName,
  81. B.PhoneNumber,
  82. B.PhoneNumberTypeID
  83. FROM #Names A
  84. LEFT JOIN AdventureWorks2019.Person.PersonPhone B ON
  85. A.BusinessEntityID = B.BusinessEntityID
  86.  
  87. --b) Add values with Update and Join:
  88. UPDATE #Master
  89. SET PhoneNumberType = B.Name
  90. FROM #Master A
  91. LEFT JOIN AdventureWorks2019.Person.PhoneNumberType B ON
  92. A.PhoneNumberTypeID = B.PhoneNumberTypeID
  93.  
  94. UPDATE #Master
  95. SET EmailAddress = B.EmailAddress
  96. FROM #Master A
  97. LEFT JOIN AdventureWorks2019.Person.EmailAddress B ON
  98. A.BusinessEntityID = B.BusinessEntityID
  99.  
  100. SELECT * FROM #Master
  101. ORDER BY #Master.BusinessEntityID
  102.  
  103. DROP TABLE #Names
  104. DROP TABLE #Master
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement