All right, welcome back to module seven. This is less than 7.6 is the first challenge of this module. And just to recap over the bullet points, challenges are based on real world requests.
You should have read the requirements document and do attempt to solve before viewing the solution. We, of course, will walk through the solution as I reach it. And remember, there are multiple ways to reach the same answer. The most important thing is that you derive the correct answer in your solution and without letting complexity get out of control. Hopefully,
and in this child's, we will create a sequel data function. So let's go ahead and go over the requirements document.
Welcome to the Module seven Course Challenge document. This Is it. The Intermediate Challenge, one of Model seven.
The sequel program requirements are as follows, utilizing the axiomatic, a database jury and employee information function that generates useful data for a programmer without having to write complex queries. So the idea is that this function is a function that a sequel programmer might use to get employee data so that they do not have to go through the trouble
of writing the joints or identifying the tables involved in getting that data.
The function requirements are that the function name be employee data. The function parameters are the company i d. The B account I D, which is also known as Thean Employee i. D.
And then the request parameter is a text field that the programmer passes in and is the actual request for the type of data that they are looking for. So if they pass in the word key that returns the employees Natural Key or the B account dot account CD field
Theseus per visor if the word supervisor is passed in and returns the employee's supervisor's full name,
if employed is passed in,
it returns. True if employed falls. Otherwise, this is determined by the presence of an open position.
And then finally,
if the word salary is passed in, it returns to employees. Current or most recent salary
and the specific data type that's returned is a string or bar chart 32 the reason we're returning of Arch are 32 is because we are returning various data types and a string concussed for those various data types. For example, the key will be a string field supervisor will be a string field.
The salary will be a decimal field
and employed could be a Boolean field. But we're going to return their words true or false. The actual word
now with that said, Let's go ahead and work through a solution to this problem.
All right, let's get started on that solution.
I have the bare bones of the function up on the screen.
I don't have the meat of the function. We're going to develop that together. But let's walk through what I do have here at the very top. I dropped function If exists, Akeem attica dot employees underscored data, and I'm being explicit with the call here. So
I named the database, and then I named the Function. Now, if I wanted Teoh, this would work just fine, too.
But it's OK to be explicit. In fact, it will work in some cases where it might fail otherwise. For example, if I didn't have the database selected, this would return it air because it wouldn't know which database to create the functioning
or where to drop the function, but with an explicit naming technique. Even if I don't have a database selected, it will still create the function in the correct place or drop the function in the correct place.
And then after that, we do the limiter swap that we see on the other object types, too.
We then create a function with with the name and then the parameters, which is the company I D. Which is a manager. The employee I D. Which is an imager. And the request, which is a character size of 12 which means that a character string of length up to 12 would be passed in. Of course, if you exceed that for any reason,
the other or the extra characters would be truncated.
Then we return a character size of 32 the same is true. If we return a string that would be too long for that. Like 34 characters, we would truncate those last two characters.
The function is deterministic, and that's telling the database that if the same parameters air passed in that it can expect the same answer.
With that said, Let's go ahead and take a quick peek at that requirements document and see what we need to develop
so we can see that the key returns employees natural key from the B account account See Field. So that's going to be any pretty easy query
supervisors gonna be a little bit more difficult employed will be a little bit more difficult and the salary will be even a little bit more difficult than the previous one. So this is actually a good order to actually complete these requests and as well, because they climb up in difficulty as we go through them. So let's go ahead and start with that key request.
We're going to start with a if statement. If requests equals key,
we'll go ahead and end the f here, although I'm really gonna do an else if, but I'm just doing that. So I contest each component as I move through the function because I'm gonna test each component so that I don't have a bunch of problems at the end. I'm Onley ever dealing with one problem at a time, and that's as they occur.
So I'm gonna start with a select star now. This would happen after the function creation, and this is just my way of testing. Select star from BP employees. It's a employee function, so it makes sense to do that.
And the function is employee data
on a company. I d is a parameter
and the account I d.
And the first part of the function we're testing is the key.
And up here, we're going to return.
We're going to put those brackets in and end with a semicolon.
Don't miss those semicolons, otherwise it will not work.
And we need to go to the B account table and we're looking for the account CD field, So this is gonna be pretty easy. We're gonna take those company and be account parameters, and we're going to use a query and were key. Use those key fields to return the data.
go ahead and select star from the account where company I d equals
company. I d Make sure your parameter names do not match or mirror the actual name of columns. Otherwise that my single engine will get confused
and the account
the account I d
So we need the account CD
on that looks like it should work. Let's go to and rawness this whole script
on. It looks like it's working correctly because right here, we dropped the function. We then create the function
that statement ends, and then the next statement of a selection occurs, and then we're actually using the function.
So we need to change this. We're not gonna end are if there were going to do a Nelson for the next requirement,
I'm gonna end that one. And if
and it's gonna be else if request equals,
what was the next one? The next one was Theseus per visor. Billy
on the supervisor is gonna require two tables the E p A employees table and the B account table. So we need the e p A employees table because that's actually where the supervisor is indicated. If we bring up that table and take a look at it,
we can see if the supervisor i d. Is on this table.
Select star from E P employees
when an alias that with an e p.
Then I'm gonna join the account.
When a list that with a b a
on the company I d equals e p dot company I d
and e p. That's
supervisor. I d equals B ada. Be account I d. That's where the target data is, here's where we apply Air Filter, where
U Preda Company I d. Is equal to company idea
and BP that the county is equal to
the employee idea.
that's semi colon.
Then we need to be sure to get the right data here, so that's gonna be a
g A dot account name.
Let's go ahead and change this to supervisor and make sure this component is working
And yes, that's what I expect to see. So it's working.
So let's move on to the next one.
And the next one is We're asking if the individual is still employed. So I needed changes and if actually was gonna drop in an else If right here helps
So for this one, we need the E p A employees position table,
and, um, that's actually it. So how can we get whether or not they're employed? What, We're gonna look for the presence of a position being active, So let's take a look at this table.
so we can see that there is. This is active column,
so we're not actually going to check for whether or not the date makes sense, We're just going to check that the is active column is true now. This kind of choice would come down to application. Design is, can you depend on this column? Or does this column mean what we believe it means? And that if
somebody has an active position than they are an active employees
and sometimes within the application, it's being used differently at different companies and requires some insight in business rules on how you would take that approach? Now, that said, in this scenario, we're gonna just treat it as if the is active flag indicates that the person has an active position.
For example, if they did not, it would be a zero, which would mean false,
and then they would have no positions with a one.
Now, with that said, how was the best way to return return return that that's true or false?
Well, we could use an aggregate function where we count the number of active fields, present foreign employees, and we could return true if it's greater than zero and false otherwise,
and I think that might be a good way to go. So let's go ahead and implement that.
we're going to select
Star from BP employees Position
Company I d equals company
on this score. I D and
employee I d
equals employees idea.
Now we still need to get that case statement correct. So let's go ahead and put that in there.
count, just in case we get more than one
really would make sense for them to have more than one active position. But just in case this function will still
function correctly, then
so we're using an aggregate function. We're counting the results
when those results are greater than zero. So one or more. Then we return true. Otherwise we return false. So let's go ahead and run that function
I don't think there's any.
I don't think there's any examples of an employee not being employed in this sample data. We could change that.
So go here.
Um 28 91 has two positions.
Drums goes, switches other one to a zero.
I'll save that
now. He should have none.
I go ahead and run this whole script again. All right,
let's try this one.
You know what it is. I forgot the last requirement
and is active equal one. Let's run that.
There we go. There's are false So now it's working.
Now let's move on to the last request.
I'm dropping another else. If
request equals salary,
And what do we need to get this? Ah,
get this answer. Well, this one's a little bit more complicated,
and that is because we actually need to select the maximum effective date for the employees and use. That is part of our primary selection mechanism. Because we don't know how the counting staff or the administrative staff are gonna move these records around.
It's possible that they could take a
a salary that was put in later and move it to a date range. That's before the previous salary. So we can't just use the max I d.
So let's take a look at what this table looks like. This is the
E. P. A. Employees rate table.
Okay, so we have a rate I d. You have employee I D.
So the way we need to solve this is that we need to get the effective date the maximal effective date for an employee and then use that date as a selection mechanism and the primary query to select that company i d. That employee i d. And that maximum effective i d to get the most recent annual salary.
there is some data missing from the annual salary. That's not a concern for the requirements. So even if we return no back, that means that that user in this application doesn't have an annual salary and
that we're still within our scope of the request, even though we may notice that
in some cases this function we return No, and depending on who you are discussing this with that may or may not make sense.
what we need to do
is let's create the query that will select the maximum effective date. First, let's break this apart. So let's select that maximum effective date. So we're gonna need the max function,
and we're gonna need the effective date
E p. A employees rate
where a company I d equals company I D
and employee I d equals employee. I d.
Okay, so with this query, we have a piece of the puzzle. We have that maximum effective date which will make sure that we get their most recent salaries effective date or start date.
So that's not the whole, um, the answer that we need. So let's right the primary query and we're gonna need this
query to filter the primary results.
So we're gonna return, put another bracket and
go ahead and select Star.
Actually, I know what I need to select annual salary
from E P Employees Rate.
Company I the equals company
equals employees idea.
Now we need to make use of that. Query wrote.
Let's go and start the filter. Constraint and
effective date is equal to
our answer for this one.
So we're using a sub query
to get the one of the filters of the primary query.
And well, that will make sure is that we always get the most recent or the most the most recent salary, which was part of the requirements.
And let's go ahead and give that a test
we're probably gonna see some Knowles which we expect to see,
and we dio and that was our the answers I expect to see.
So that completes this challenge Now the trickiest. One was the salary one because the best way to do that was actually use a sub query.
And that's because you could see a scenario and you don't see it in the sample sample data. Unfortunately, where people have multiple salaries as they have evolved through the company, they've gotten raises every year, and that causes them to have multiple salaries. So you have to have a way of getting that most recent salary.
Now that said, that completes his challenge. I hope you enjoyed working through the solution, and I hope to see you in the next challenge slash lesson. Thank you.