Sie sind auf Seite 1von 15

QUESTION 1

As part of a yearly fundraising effort for the community, your manager has given you an Excel
worksheet titled "Donations." It includes all employees' pledged donations for the year. The
sheet consists of three tabs that correspond to the 3 months when employees were asked to
submit pledges. Load this file into the AdventureWorks database through the Import and
Export Wizard. Once loaded, use a UNION ALL statement to combine the three data sets into
a temporary table called "#TotalDonations."

CODE:
CREATE TABLE #TotalDonations (EmployeeID int, Donation int);
INSERT INTO #TotalDonations
SELECT *
FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 1]
UNION ALL
SELECT *
FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 2]
UNION ALL
SELECT *
FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 3]

SELECT * FROM AdventureWorks2016CTP3.#TotalDonations

QUESTION 2
Redoing the UNION ALL statement you created in Problem One, create a temporary table
looking exclusively at "SUM of Donations by Month." Title each of the three fields "Donations
and Month" with the temporary table titled "#DonationsbyMonth." When complete, provide
the total of the final amount of donations raised.

CODE:
CREATE TABLE #TotalDonations (EmployeeID int, Donation int);
INSERT INTO #TotalDonations
SELECT * FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 1]
UNION ALL
SELECT * FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 2]
UNION ALL
SELECT * FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 3]

CREATE TABLE #DonationsbyMonth (DonationsMonth1 int, DonationsMonth2 int, DonationsMonth3 int, TotalDonations int);
INSERT INTO #DonationsbyMonth
SELECT
(SELECT SUM([Donation]) FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 1]),
(SELECT SUM([Donation]) FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 2]),
(SELECT SUM([Donation]) FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 3]),
(SELECT SUM([Donation]) FROM #TotalDonations)
SELECT * FROM #DonationsbyMonth

QUESTION 3
Your manager e-mails you about the final results you provided in Problem Three, and believes
you must have made a mistake when calculating the total donations. The amount is $100.00
less than the total amount calculated by the manager. The manager thinks it is possible that
one employee might have accidently pledged $100.00 twice. Explain how this event might
occur with a UNION ALL. Review the UNION ALL, and if there is an error, rewrite the first
query with a UNION statement. Discuss whether the manager's assumption was correct and
explain why.

CODE:
CREATE TABLE #TotalDonations (EmployeeID int, Donation int);
INSERT INTO #TotalDonations
SELECT DISTINCT * FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 1]
UNION ALL
SELECT DISTINCT * FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 2]
UNION ALL
SELECT DISTINCT * FROM AdventureWorks2016CTP3.dbo.[MIS-610-RS-Donations 3]

SELECT * FROM #TotalDonations

QUESTION 4
You receive an e-mail request from Human Resources requesting information about the last
time a pay rate was changed for an employee. The employee's "NationalIDNumber" is
486228782. Develop a subquery instead of an INNER JOIN to provide the answer to this
request.
Note: A subquery is another method of writing a JOIN. It can be efficient when used at the
correct times.

CODE:
SELECT BusinessEntityID, RateChangeDate FROM AdventureWorks2016CTP3.HumanResources.EmployeePayHistory
WHERE BusinessEntityID IN (
SELECT BusinessEntityID FROM AdventureWorks2016CTP3.HumanResources.Employee
WHERE NationalIDNumber = 486228782
)
nth3 int, TotalDonations int);
EmployeePayHistory

es.Employee
EmployeeID Donation
1 300
5 125
11 25
13 100
22 25
23 125
25 100
26 25
30 250
33 200
34 100
35 50
40 200
45 150
46 125
47 100
48 200
50 225
51 25
53 25
55 125
56 50
58 25
59 125
61 25
64 225
65 50
66 225
71 150
74 100
76 25
77 50
79 200
80 225
83 225
84 25
86 150
92 25
95 125
101 275
105 25
106 125
107 125
108 100
112 125
113 125
114 150
116 125
120 200
121 125
123 50
125 125
126 125
136 25
138 125
140 125
141 25
146 100
150 25
152 100
160 125
162 100
163 50
164 125
168 200
172 225
173 100
175 100
179 125
184 100
185 125
189 150
190 25
192 100
194 100
198 125
202 125
205 50
224 25
229 125
234 100
235 100
236 50
239 125
240 125
241 125
242 225
243 50
247 125
249 100
250 125
252 200
255 125
259 200
260 25
262 100
263 50
264 100
265 125
270 25
276 100
278 100
279 100
281 25
282 125
285 125
286 200
1 125
4 100
6 100
12 125
13 25
15 125
16 25
18 225
19 125
20 100
21 25
25 175
26 100
28 25
29 125
31 25
34 25
35 75
37 125
43 150
44 125
46 125
47 200
56 100
58 350
62 125
63 25
65 150
66 100
67 25
73 150
74 25
79 150
82 150
83 25
86 150
89 225
91 125
95 100
96 125
99 125
100 125
101 250
102 200
103 100
104 25
108 50
110 100
111 200
112 125
113 25
115 150
117 25
119 125
120 25
121 25
128 150
130 125
132 25
133 125
134 225
135 25
138 250
140 150
142 125
144 100
148 125
152 100
153 125
157 100
161 125
163 25
164 125
166 250
167 125
172 100
174 25
175 50
177 225
181 50
184 25
185 150
186 100
191 125
192 125
194 25
195 200
198 25
199 25
200 100
204 25
207 100
208 100
209 125
210 200
211 100
215 125
216 100
218 25
221 125
223 100
224 25
229 25
232 100
233 100
234 250
237 50
243 150
260 125
261 50
262 25
263 100
268 25
269 125
270 225
271 125
277 125
278 125
279 25
280 225
282 125
284 200
285 100
286 25
287 25
289 100
4 150
10 50
13 100
16 200
20 150
23 225
29 100
31 25
37 25
39 25
41 25
43 25
44 150
46 25
47 150
48 100
49 125
50 100
53 225
56 125
58 100
59 25
60 100
61 125
64 225
68 50
69 100
71 125
72 25
76 50
80 225
82 150
83 25
84 150
88 150
89 125
90 125
92 125
93 225
94 300
95 100
96 125
100 25
101 125
105 25
107 125
109 125
112 125
118 25
120 225
121 100
123 100
126 150
130 300
132 300
133 100
136 125
137 325
138 25
139 25
141 250
145 25
146 125
155 100
156 100
157 100
162 25
165 25
168 100
169 225
174 25
175 100
177 125
181 150
184 25
185 100
186 150
187 25
188 25
191 25
194 25
195 25
196 125
197 100
199 100
199 100
203 125
204 25
205 125
208 100
212 25
214 350
220 25
221 125
222 25
228 25
231 25
232 125
233 100
238 50
239 125
240 50
241 125
245 125
248 100
249 25
250 25
251 25
255 250
256 150
259 150
261 200
274 200
276 25
279 25
281 125
282 25
284 25
287 125
288 25
290 25
DonationsMonth1 DonationsMonth2 DonationsMonth3 TotalDonations
12125 13600 12700 38425
DonationsMonth1 DonationsMonth2 DonationsMonth3 TotalDonations
12125 13600 12700 38325
BusinessEntityID RateChangeDate
13 12/23/10 12:00 AM

Das könnte Ihnen auch gefallen