Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
0:00
what is going on everybody my name is
0:02 Alex Freiburg and today we're gonna be 0:03 walking through K statements and sequel 0:05 a case statement allows you to specify 0:08 condition and then it also allows you to 0:10 specify what you want returned when that 0:13 condition is met so we're gonna be using 0:15 this employee demographics table that 0:17 we're looking at right here we're gonna 0:19 walk through the syntax of how to create 0:20 a cait statement and then we're actually 0:22 going to some use cases at the end so 0:25 let's start off by specifying what 0:26 columns we want let's say we want the 0:29 first name we want the last name and we 0:34 want the age now let's just get that 0:37 information now for our case statement 0:40 we're gonna be using this age column so 0:42 we actually want the age to be in there 0:43 so let's specify where age is not know 0:48 and run that so now we have a pretty 0:51 good look at it and let's just order by 0:55 age just to clean it up a little bit so 1:00 now I'll start building our case 1:01 statement so we're gonna say case and 1:03 then we want to say when now we need to 1:07 specify what condition we want to look 1:09 for so let's do when age is greater than 1:13 30 then then what do we want to be 1:16 returned so we want to return that they 1:19 are old else so that means anything that 1:23 is not over the age of 30 we want to 1:25 return young and then you need to 1:30 specify that you were done with the case 1:31 statement and so you will write and at 1:34 the very bottom so this is our first 1:36 case statement let's run it and see what 1:38 we get so as you can see a new column 1:40 was created and if the person is over 1:43 the age of 30 so 31 and up they're given 1:46 old and if they're not over the age of 1:48 30 they are given young now we can do as 1:52 many win and then statements as we want 1:54 so if we want to we can also do when the 1:57 age is between 27 and 30 then we want to 2:05 return young and anyone else where they 2:09 call a 2:11 so now we have Ryan Howard as the baby 2:15 anyone between 27 and 30 they're 2:18 considered a young and anyone over the 2:21 age of 30 is old now something to note 2:24 is that the very first condition that is 2:26 met is going to be returned so if there 2:29 are multiple conditions that meet the 2:31 criteria only the very first one is 2:33 going to be returned and let's 2:34 demonstrate that real quick so if the 2:37 age equals 38 then return Stanley 2:43 because that is Stanley and let's 2:47 execute this real quick 2:48 so right here I'm specifying that if 2:51 it's 38 it should return Stanley but he 2:53 is right here and it still says old and 2:56 that's because this condition was 2:57 already met now if we were to put this 3:00 right here it should work correctly and 3:06 let's try it out so now because this 3:08 condition is met first it is going to 3:10 return Stanley down here so now let's 3:13 get into our first use case let's start 3:15 off by copying this and then commenting 3:19 it out I only did that because I don't 3:22 want to rewrite it cuz I'm lazy let's 3:26 get rid of that and let's look at this 3:28 real quick 3:28 we are gonna join on another table that 3:30 we have really fast and that's gonna be 3:34 sequel tutorial if you watch my other 3:36 videos then you know this table and 3:40 we're gonna do that on employee 3:42 demographics employee ID is equal to an 3:48 employee salaried employee ID okay so 3:54 let's just look at everything in these 3:56 tables really quick now we are gonna be 3:57 focusing on the job title in the salary 3:59 column but we want their first name and 4:01 last name as well so let's start 4:02 building that out let's do first name 4:06 last name job title and salary and let's 4:12 look at this really quick so now we have 4:14 our employees and here is the situation 4:16 we had a fantastic year this year 4:19 selling paper and corporate has allowed 4:20 Michael Scott to give out a yearly raise 4:23 to every sing 4:23 employee but not every employee is gonna 4:26 get the same raise because our salesmen 4:29 are genuinely the people who made us our 4:31 money and they're gonna get the biggest 4:33 raises well other people really aren't 4:35 gonna get into that big of a raise so 4:37 now let's go through and create a case 4:38 statement to calculate what their salary 4:40 will be after they get their raise so 4:43 let's start off by saying case and when 4:47 and we want it to say when job titles 4:51 equal to salesmen so when they are a 4:54 Salesman what do we want to happen so 4:57 this is where the calculation occurs so 4:59 we're gonna take their salary and then 5:02 we're gonna add their salary times how 5:06 much their raise is gonna be so the 5:08 salesman did really really well and we 5:10 want to give them a 10% raise this year 5:12 now when their job title is equal to 5:19 accountant then and we'll take their 5:23 salary we will give them let's give them 5:29 a 5% raise it's still very generous 5:32 there we go and when the job title is 5:40 equal to H R then it's gonna be the 5:45 salary plus the salary times and then 5:52 we're gonna do one all right and else 5:57 we're just gonna do salary plus salary 6:03 oops 6:04 let's do parentheses times and let's 6:08 just give everyone else a 3% raise and 6:12 then we'll write end now let's take a 6:15 look at our results so here's what we 6:18 have so far we have our first name or 6:20 last name our job title in our salary 6:22 that is our current salary and then 6:25 we're gonna have our salary after we get 6:27 our raise so I'm going to actually write 6:29 that up here let's do as salary after 6:33 raise and let's execute that 6:37 so let's look at these raises really 6:39 quick so we have 45,000 and since he is 6:42 a Salesman he gets a 10% raise which is 6:45 a raise of $4,500 6:47 so 45,000 plus 4,500 is 49 thousand five 6:51 hundred dollars and as you can see down 6:53 here we have HR who is making fifty 6:55 thousand dollars is now he is making 6:57 fifty thousand dollars and five cents so 6:59 everybody got a raise so that is our 7:03 case statement I hope that was helpful I 7:04 find myself using the case statement a 7:06 lot when I'm wanting to categorize 7:07 things or label things and that's kind 7:10 of what we did in the first example and 7:12 you can even do calculations like we did 7:14 in this use case so I hope that was 7:16 helpful thank you guys so much for 7:18 watching I really appreciate it 7:20 if you learned anything from this video 7:22 be sure to like and subscribe below and 7:24 I'll see you in the next video