Repository navigation
Expand file tree
/
Copy path14_ScalarUDFs_Homework.sql
More file actions
225 lines (109 loc) · 5.33 KB
/
Copy path14_ScalarUDFs_Homework.sql
File metadata and controls
225 lines (109 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/******************************************************************************
Course videos: https://www.red-gate.com/hub/university/courses/t-sql/tsql-for-beginners
Course scripts: https://litknd.github.io/TSQLBeginners
Scalar User Defined Functions (UDFs)
HOMEWORK FILE
*****************************************************************************/
/* Doorstop */
RAISERROR(N'Did you mean to run the whole thing?',20,1) WITH LOG;
GO
USE WideWorldImporters;
GO
--Run this to simplify execution plans
--https://littlekendra.com/2017/01/26/whats-that-garbage-in-my-execution-plan-dear-sql-dba-episode-26/
DROP SECURITY POLICY IF EXISTS [Application].[FilterCustomersBySalesTerritoryRole];
GO
/*
Q1
Script out the definition of the function [Website].[CalculateCustomerPrice] to a new window
(This is a function that ships with the WideWorldImporters database)
Can you see why this would need to execute once per row?
You don't have to write a query for this question, just get familiar with the function,
you will be using it in the next questions.
*/
/*
Q2
Write a query using the [Website].[CalculateCustomerPrice] function in the WHERE clause:
Using the Sales.Invoices, Sales.InvoiceLines and Sales.Customers tables
Return one column: [Count Over 1K], defined as the count of rows where...
The [Website].[CalculateCustomerPrice] function returns a price > 1000 for:
That Customer.CustomerId, InvoiceLines.StockItemID, and the hardcoded date 2016-04-01
This should return one row.
*/
/*
Q3
Build on the query in the previous question...
Write a query using the [Website].[CalculateCustomerPrice] in multiple parts of the query.
Using the Sales.Invoices, Sales.InvoiceLines and Sales.Customers tables
Return three columns:
CustomerName (from Sales.Customers)
CustomerPrice, defined as the calculation from [Website].[CalculateCustomerPrice] for...
That Customer, The StockItemID on that InvoiceLines item, and the hardcoded date 2016-04-01
InvoiceLineCount, the count of InvoiceLines for that customer at that price
Return only rows where the [Website].[CalculateCustomerPrice] function returns a price > 1000 for:
That Customer.CustomerId, InvoiceLines.StockItemID, and the hardcoded date 2016-04-01
Group the rows by:
CustomerName (from Sales.Customers)
CustomerPrice (as defined above)
Order the rows by CustomerPrice DESC
This should return 532 rows.
*/
/*
Q4
This query uses the same tables as the previous two questions, it may be useful to copy and paste your FROM/JOINs
Write a query that validates the Sales.InvoiceLines.UnitPrice column
Using the Sales.Invoices, Sales.InvoiceLines and Sales.Customers tables
Return one column:
InvoiceLineId
Return only rows where
The UnitPrice for that row does NOT equal:
The calculation returned by [Website].[CalculateCustomerPrice] for that Customer, That StockItemID, and that InvoiceDate
This should return zero rows
*/
/*
Q5
This one is a 3 parter: write a function, write a query using the function, write the equivalent query NOT using the function.
Part 1:
Write a scalar user defined TSQL function named dbo.InvoiceCount
Takes an integer parameter for CustomerID
Returns an integer
Counts the number of invoices in the Sales.Invoices table for that CustomerID
Returns the count of those invoices
Part 2:
Write a query with the function
Using the Sales.Customers table and dbo.InvoiceCount()
Query returns the top 10 rows based on the InvoiceCount for that CustomerID (using the function)
Returns two columns:
CustomerName
InvoiceCount, defined as the count of invoices for that customer as determined by dbo.InvoiceCount()
Part 3:
Write an equivalent query that doesn't use dbo.InvoiceCount()
Instead, use both the Sales.Customers and Sales.Invoices tables
*/
/*
Q6
This one is also a 3 parter: write a function, write a query using the function, write the equivalent query NOT using the function.
Part 1:
Write a scalar user defined TSQL function named dbo.Initials that doesn't do data access
Takes a parameter @FullName, NVARCHAR(100)
Returns CHAR(4)
Create the function WITH SCHEMABINDING
Determines and returns the initials for the FullName and returns them in the format: F.L.
Some built in functions that may be helpful determining the initials:
LEFT ( character_expression , integer_expression )
SUBSTRING ( expression , start , length )
CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )
Keep this simple and assume:
* You only care about returning two initials for any name
* There is only one space in FullName, and it is between the first name and last name
* Do not worry about capitalization, return the initials as they are in FullName
Part 2:
Write a query with the function
Using the Application.People table and dbo.Initials()
Returns two columns:
FullName
Initials, defined as the initials for that fullname calculated by dbo.Initials()
Part 3:
Write an equivalent query that doesn't use dbo.Initials()
Instead, calculate the initials for each fullname in the query itself using only built-in functions
*/