From the course: Advanced SQL for Data Science: Time Series

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Lag

Lag - SQL Tutorial

From the course: Advanced SQL for Data Science: Time Series

Start my 1-month free trial

Lag

- [Instructor] Another windowing function we want to take a look at is called Lag. It's like Lead, but it refers to rows that occur before the current row. So rather than type everything in again, I'm going to navigate up to my chapter five exercise folder, and I'm going to open up Lag.SQL, and let's just take a quick look at this. So what we have here is similar to what we had before. We are selecting our department ID, our server ID, and CPU utilization, and then we want to look at the CPU utilization that came before the row. So for example, let's execute this. Now what you'll notice here, the first row doesn't have a predecessor, so its Lag value is null. But the other rows contain the value that appeared in the row before. Now, like the Lead function, you can change the offset. So for example, if we wanted to look at a Lag of ten, then we can add ten to the Lag function call and execute, and you'll notice that the first ten rows have no predecessors, so they're all no. But…

Contents