Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:01,024 --> 00:00:02,560
Hi everyone
1
2
00:00:03,328 --> 00:00:04,352
In this video
2
3
00:00:04,608 --> 00:00:06,912
We'll learn about case expression
3
4
00:00:07,680 --> 00:00:10,496
Case expression is a conditional expression
4
5
00:00:11,008 --> 00:00:17,152
What is mean this if you want to specify several conditions on your data
5
6
00:00:17,408 --> 00:00:17,920
While
6
7
00:00:18,176 --> 00:00:20,224
Retrieving it or updating it
7
8
00:00:20,736 --> 00:00:24,832
You can do it using the case expression
8
9
00:00:25,344 --> 00:00:28,416
It is similar to mentioning the if else statements
9
10
00:00:28,672 --> 00:00:32,512
While coding, so if you have done C , C++ or java
10
11
00:00:33,792 --> 00:00:39,936
What you achieve by using if and else statements similar thing you can do using the case statement
11
12
00:00:40,192 --> 00:00:45,312
The syntax of case statement is you write case
12
13
00:00:45,568 --> 00:00:47,616
Then you mention when
13
14
00:00:47,872 --> 00:00:51,456
Specify the first condition if it is true then
14
15
00:00:51,712 --> 00:00:52,480
The result
15
16
00:00:53,760 --> 00:00:59,648
You can again mention the second when, where you specify the second condition and if that is true
16
17
00:00:59,904 --> 00:01:01,184
It will give the result
17
18
00:01:01,696 --> 00:01:03,488
3rd, 4th and so on
18
19
00:01:04,000 --> 00:01:10,144
And in the end you can specify else so if none of the condition specified above are true
19
20
00:01:11,424 --> 00:01:12,448
The else will run
20
21
00:01:13,216 --> 00:01:16,800
And whatever result you mentioned within this else clause
21
22
00:01:18,080 --> 00:01:19,872
That will be executed
22
23
00:01:20,640 --> 00:01:22,432
And to end the
23
24
00:01:22,688 --> 00:01:24,992
Case expression we write end
24
25
00:01:26,784 --> 00:01:32,928
There is another way to use case, you will write case and then mention an expression
25
26
00:01:33,184 --> 00:01:35,232
This expression can be a column name
26
27
00:01:36,512 --> 00:01:37,792
For each value in the column
27
28
00:01:38,048 --> 00:01:42,144
This condition will be checked and wherever this condition is true
28
29
00:01:42,400 --> 00:01:43,936
You will get the
29
30
00:01:44,192 --> 00:01:46,496
Result corresponding to that condition
30
31
00:01:48,544 --> 00:01:49,568
Let us look at
31
32
00:01:49,824 --> 00:01:51,872
An example to understand it further
32
33
00:01:54,432 --> 00:01:56,224
One of the major use cases of
33
34
00:01:56,480 --> 00:01:57,760
Case statement is
34
35
00:01:58,272 --> 00:02:00,576
When you want to assign categories to your data
35
36
00:02:01,344 --> 00:02:03,648
So for example, in my customer data
36
37
00:02:03,904 --> 00:02:06,208
If I want to categorise my
37
38
00:02:06,464 --> 00:02:08,768
Customer data into 3 parts
38
39
00:02:10,304 --> 00:02:13,888
Customers age Less than 30 will be categorised as young
39
40
00:02:14,656 --> 00:02:20,800
Customers aged 30 to 60 will be called middle aged and customers
40
41
00:02:21,056 --> 00:02:22,592
Beyond 60 years will be
41
42
00:02:22,848 --> 00:02:24,384
Categorised as senior citizens
42
43
00:02:25,664 --> 00:02:26,944
If I want to do this
43
44
00:02:27,968 --> 00:02:28,992
I'll use case
44
45
00:02:29,504 --> 00:02:33,344
I'll write select star so this will give me all the columns of the customer table
45
46
00:02:34,368 --> 00:02:35,136
Comma
46
47
00:02:35,392 --> 00:02:41,536
Case, so this will become a new column in after the entire table, so this column
47
48
00:02:41,792 --> 00:02:43,072
Will be named case
48
49
00:02:43,328 --> 00:02:45,888
And within this column, I want data
49
50
00:02:46,144 --> 00:02:46,912
Such that
50
51
00:02:47,168 --> 00:02:51,520
Customers who have age less 30 this column should contain young
51
52
00:02:52,288 --> 00:02:56,384
Customers with age more than 60 this column should contain senior citizen
52
53
00:02:56,640 --> 00:02:58,432
For all other customers
53
54
00:02:58,688 --> 00:03:01,248
This column should contain middle aged
54
55
00:03:02,016 --> 00:03:03,552
To finish this case statement
55
56
00:03:03,808 --> 00:03:05,088
I'll write end
56
57
00:03:06,368 --> 00:03:11,488
I can assign this whole column a name by using as age category
57
58
00:03:11,744 --> 00:03:13,024
I do not write this
58
59
00:03:13,536 --> 00:03:15,584
This column will be named as case
59
60
00:03:16,864 --> 00:03:17,632
Now
60
61
00:03:17,888 --> 00:03:22,240
Since I am using this alias this column will be named as age category
61
62
00:03:23,264 --> 00:03:23,776
From
62
63
00:03:24,288 --> 00:03:25,312
Customer table
63
64
00:03:26,080 --> 00:03:28,640
Let us go and write this query in pg admin
64
65
00:03:29,920 --> 00:03:33,248
We will select star
65
66
00:03:33,504 --> 00:03:36,576
Comma case
66
67
00:03:36,832 --> 00:03:38,880
When
67
68
00:03:40,672 --> 00:03:43,232
Age is less than 30
68
69
00:03:44,256 --> 00:03:49,632
Then output should be young
69
70
00:03:51,680 --> 00:03:52,960
When age
70
71
00:03:53,472 --> 00:03:56,544
Is more than 60
71
72
00:03:57,056 --> 00:03:58,336
Then
72
73
00:03:58,592 --> 00:03:59,872
Senior citizen
73
74
00:04:06,272 --> 00:04:07,808
Else
74
75
00:04:09,856 --> 00:04:12,672
Middle aged
75
76
00:04:17,791 --> 00:04:19,071
End as
76
77
00:04:19,327 --> 00:04:21,375
Age category
77
78
00:04:30,079 --> 00:04:32,127
From
78
79
00:04:32,639 --> 00:04:36,223
Customer
79
80
00:04:36,479 --> 00:04:39,295
Let us run this
80
81
00:04:44,415 --> 00:04:47,231
So if you scroll to the right
81
82
00:04:47,743 --> 00:04:49,279
This new column will be
82
83
00:04:50,303 --> 00:04:51,583
In the end
83
84
00:04:51,839 --> 00:04:54,143
It is named as age category
84
85
00:04:54,655 --> 00:04:55,679
And you can see
85
86
00:04:55,935 --> 00:05:00,287
For customer with age 67, it has value senior citizen
86
87
00:05:00,543 --> 00:05:02,847
For customer with age 33 it has
87
88
00:05:03,103 --> 00:05:04,127
Value Middle aged
88
89
00:05:04,639 --> 00:05:06,943
For customer with Value 20
89
90
00:05:07,455 --> 00:05:08,991
It is young
90
91
00:05:10,783 --> 00:05:14,111
This is how we use this conditional expression of case
91
92
00:05:15,647 --> 00:05:17,695
That's all for this video, thanks
6788
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.