Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,370 --> 00:00:06,230
Now the next function we have is lag or lead function.
2
00:00:07,620 --> 00:00:16,680
No lag or lead function will give you the value of previous rows in case of leg function or the next
3
00:00:16,680 --> 00:00:19,290
rows in the lead function.
4
00:00:20,090 --> 00:00:21,350
For example.
5
00:00:22,550 --> 00:00:25,790
We have these three columns customer data and revenue.
6
00:00:27,520 --> 00:00:36,250
If I use lack of one in my revenue column, it will give me the value of previous row of revenue column.
7
00:00:36,250 --> 00:00:44,800
For example, the lag of revenue one in second row will give me the value of revenue in the first row
8
00:00:44,800 --> 00:00:45,700
that is hundred.
9
00:00:45,700 --> 00:00:52,660
So I am getting this 100 over here if I am calculating the lag of one on revenue.
10
00:00:53,860 --> 00:00:59,860
Similarly, for the third row, the lag of one is 200.
11
00:01:00,950 --> 00:01:02,870
So I'm getting to 100 over here.
12
00:01:04,330 --> 00:01:10,160
Similarly for this row, the leg value with interval one is 300.
13
00:01:10,180 --> 00:01:13,150
So I'm getting 300 for this row.
14
00:01:13,300 --> 00:01:17,980
The leg of one is 300, so I'm getting 300 over here.
15
00:01:18,880 --> 00:01:23,860
So with leg you can have the previous values of any column.
16
00:01:24,880 --> 00:01:30,340
Similarly with lead, you can have the next values of particular column.
17
00:01:31,560 --> 00:01:39,030
And again, as you can see, if the leg value is not available, such as in this first row, the lag
18
00:01:39,030 --> 00:01:42,630
value is not available since there is nothing about this hundred.
19
00:01:42,660 --> 00:01:45,570
In such cases you will get a null value.
20
00:01:46,850 --> 00:01:52,160
If you look at the syntax in length, you have to give two parameters.
21
00:01:52,160 --> 00:02:00,350
First one is the column for which you want the leg value, and second is the interval interval of the
22
00:02:00,350 --> 00:02:01,130
length.
23
00:02:01,160 --> 00:02:05,030
For example, if you write one, it will just pick the.
24
00:02:05,790 --> 00:02:12,390
Above value if you write to it will pick the value that is two intervals above.
25
00:02:12,480 --> 00:02:16,530
If you write three, it will pick the value that is three intervals.
26
00:02:17,670 --> 00:02:21,510
And similarly, you can use partition by and order by.
27
00:02:22,960 --> 00:02:26,470
Partitioned by to group your data and order by.
28
00:02:27,800 --> 00:02:33,950
To order your data and to specify on which order you want to get the legs.
29
00:02:34,310 --> 00:02:35,480
All leads.
30
00:02:36,450 --> 00:02:40,350
Now let's go back to our page admin and calculate like.
31
00:02:44,320 --> 00:02:51,790
So suppose if in this data I want to calculate the lag values on customer orders.
32
00:02:52,000 --> 00:03:01,210
So for each order, I want to calculate what was the order value of the previous order from that particular
33
00:03:01,210 --> 00:03:01,900
customer.
34
00:03:01,930 --> 00:03:08,950
So suppose if one customer is ordering multiple times, I want to add a column which will specify what
35
00:03:08,950 --> 00:03:13,660
was the order value of the previous order that customer has placed.
36
00:03:15,770 --> 00:03:17,630
So let's see how to do that.
37
00:03:18,350 --> 00:03:23,930
All right, select now we want customer ID.
38
00:03:27,380 --> 00:03:29,150
We want Order.
39
00:03:29,150 --> 00:03:29,900
Date.
40
00:03:36,640 --> 00:03:38,050
Or that I'd.
41
00:03:41,160 --> 00:03:42,420
Sales value.
42
00:03:44,780 --> 00:03:47,360
And then leg.
43
00:03:48,430 --> 00:03:49,840
Of sales value.
44
00:03:51,860 --> 00:03:53,570
I want the previous order.
45
00:03:53,570 --> 00:03:56,930
So leg of one.
46
00:04:00,930 --> 00:04:01,800
Over.
47
00:04:03,340 --> 00:04:05,130
Partition by customer.
48
00:04:05,140 --> 00:04:08,200
So I want to do this analysis on customer bases.
49
00:04:08,200 --> 00:04:13,930
I want to calculate for each customer what was their previous order value.
50
00:04:13,930 --> 00:04:16,330
So that's why partition by customer.
51
00:04:19,910 --> 00:04:20,600
I'd.
52
00:04:21,970 --> 00:04:23,620
And order by.
53
00:04:25,320 --> 00:04:26,190
Order date.
54
00:04:27,710 --> 00:04:31,340
So my orders can be arranged using the order date.
55
00:04:32,140 --> 00:04:32,890
So.
56
00:04:33,100 --> 00:04:33,840
Well, right.
57
00:04:34,210 --> 00:04:35,080
Ordered by order.
58
00:04:35,080 --> 00:04:35,590
Date?
59
00:04:36,550 --> 00:04:37,210
Yes.
60
00:04:38,910 --> 00:04:40,020
Previous.
61
00:04:40,810 --> 00:04:43,000
See, this is the alias.
62
00:04:43,510 --> 00:04:45,790
Similarly, we can also.
63
00:04:47,260 --> 00:04:48,910
The previous order.
64
00:04:48,910 --> 00:04:49,390
I'd.
65
00:04:49,510 --> 00:04:51,460
So I will just copy it.
66
00:04:51,970 --> 00:04:54,850
I will calculate the lag on order ID.
67
00:04:57,960 --> 00:05:08,280
The interval is one partition by order, by order, date and this time write previous order ID.
68
00:05:14,110 --> 00:05:14,920
From.
69
00:05:16,780 --> 00:05:18,640
Murder roll up state.
70
00:05:20,930 --> 00:05:22,160
Let's run this.
71
00:05:25,950 --> 00:05:27,000
You can see.
72
00:05:29,010 --> 00:05:30,500
For some variety.
73
00:05:30,510 --> 00:05:32,640
This 10315.
74
00:05:33,330 --> 00:05:34,260
There are.
75
00:05:35,200 --> 00:05:36,400
Five orders.
76
00:05:37,840 --> 00:05:38,680
This fight.
77
00:05:40,280 --> 00:05:45,080
These orders are arranged by order date since we are mentioned.
78
00:05:45,110 --> 00:05:47,930
Order by order, date and window function.
79
00:05:48,970 --> 00:05:55,900
P have the sales value of each order and we have the leg value stored as previous sales and previous
80
00:05:55,900 --> 00:05:56,250
order.
81
00:05:58,240 --> 00:06:03,070
So for the first row there is no previous order ID, order sales or order.
82
00:06:03,370 --> 00:06:04,690
So we are getting null.
83
00:06:05,900 --> 00:06:07,310
For the second order.
84
00:06:07,520 --> 00:06:16,220
The previous sales amount was 7 to 6, and the previous order ID was this 128055128055.
85
00:06:16,520 --> 00:06:19,160
So we are getting the leg values over here.
86
00:06:20,410 --> 00:06:28,240
If we jump to the next customer for the next customer, the first value is null since there are no previous
87
00:06:28,240 --> 00:06:30,640
order for this particular customer.
88
00:06:30,670 --> 00:06:34,540
So these two columns are null.
89
00:06:35,020 --> 00:06:43,840
And from the next column we are getting the data of previous order, which is sales 16.52 and.
90
00:06:44,730 --> 00:06:48,690
Or that it which is this you can also use.
91
00:06:49,660 --> 00:06:51,040
Different intervals.
92
00:06:51,040 --> 00:06:51,820
So.
93
00:06:52,680 --> 00:06:58,310
If you want to calculate the lag of two, you can also do that.
94
00:06:58,320 --> 00:07:05,880
In that case, your first two rows will be null and in the third row you will get the information of
95
00:07:05,880 --> 00:07:06,900
your first row.
96
00:07:09,120 --> 00:07:12,720
So that's how you can use lag or lead.
97
00:07:13,230 --> 00:07:14,970
Let me show you lead as well.
98
00:07:20,400 --> 00:07:24,240
In lead, you will get the data of next rose.
99
00:07:24,240 --> 00:07:24,990
So.
100
00:07:34,620 --> 00:07:42,270
So here you can see if you are using lead, you will get null for the last row of any particular customer
101
00:07:42,480 --> 00:07:47,730
and in the end, in the other rows you will get the information of next rows.
102
00:07:47,730 --> 00:07:49,710
For example, in first row.
103
00:07:49,740 --> 00:07:52,380
See, the next sales is 29.5.
104
00:07:52,380 --> 00:07:58,080
So you are getting 29.5 where the next order it is 138100.
105
00:07:58,080 --> 00:08:01,140
So you are getting 13100 here.
106
00:08:01,440 --> 00:08:05,040
So that's how you can use lag and lead functions.
107
00:08:05,250 --> 00:08:14,670
Lack functions are very important if you are working with time, series data and lags are usually used
108
00:08:14,670 --> 00:08:16,740
for forecasting as well.
8697
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.