Skip to content

Commit

Permalink
Add example and usecase using Partition by (#5677)
Browse files Browse the repository at this point in the history
* Add example using partition by

* Addressed admin feedback: Tables format and headers

* Update content/sql/concepts/window-functions/terms/lag/lag.md

* Fix formatting of SQL queries

* Update lag.md

* Update lag.md

* Update lag.md

* Update lag.md

* Update lag.md

* Update lag.md

* Fix lint errors

---------
  • Loading branch information
gustavo-crespo authored Dec 16, 2024
1 parent 07ec82a commit e86127f
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions content/sql/concepts/window-functions/terms/lag/lag.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,63 @@ Users Table
| kyle | xy | 60 |

```sql
SELECT *,
SELECT
first_name,
last_name,
age,
LAG(age, 1) OVER (
ORDER BY age ASC) AS previous_age
ORDER BY age DESC
) AS previous_age
FROM Users;
```

The output is a table that features a new column `previous_age`, which holds the values from the previous records. The first record is null because a default was not specified and the previous row would be out of range.

Output
The output of the above code is a table that features a new column `previous_age`, which holds the values from the previous records. The first record is null because a default was not specified and the previous row would be out of range.

| first_name | last_name | age | previous_age |
| ---------- | --------- | --- | ------------ |
| kyle | xy | 60 | null |
| kyle | xy | 60 | NULL |
| jenna | black | 35 | 60 |
| chris | smith | 30 | 35 |
| dave | james | 19 | 30 |

### Using `PARTITION BY` Clause

This example demonstrates how to use the `LAG()` function to create a new column, `previous_position`.

The `PARTITION BY employee_id` clause ensures that the `LAG()` function operates within each group of rows that share the same `employee_id`. The `ORDER BY promotion_date` ensures the rows are processed in chronological order.

`Promotions` Table

| employee_id | promotion_date | new_position |
| ----------- | -------------- | ------------ |
| 1 | 2020-01-01 | Junior Dev |
| 1 | 2021-06-01 | Mid Dev |
| 1 | 2024-03-01 | Senior Dev |
| 2 | 2019-05-01 | Intern |
| 2 | 2022-11-01 | Analyst |
| 2 | 2024-11-20 | Data Analyst |

```sql
SELECT
employee_id,
promotion_date,
new_position,
LAG(new_position) OVER (
PARTITION BY employee_id
ORDER BY promotion_date
) AS previous_position
FROM Promotions;
```

Within each group defined by `employee_id`, the `previous_position` column holds the value from the previous row based on `promotion_date`. The first record in each group is `NULL` because there is no preceding row.

The above code generates the following output:

| employee_id | promotion_date | new_position | previous_position |
| ----------- | -------------- | ------------ | ----------------- |
| 1 | 2020-01-01 | Junior Dev | NULL |
| 1 | 2021-06-01 | Mid Dev | Junior Dev |
| 1 | 2024-03-01 | Senior Dev | Mid Dev |
| 2 | 2019-05-01 | Intern | NULL |
| 2 | 2022-11-01 | Analyst | Intern |
| 2 | 2024-11-20 | Data Analyst | Analyst |

0 comments on commit e86127f

Please sign in to comment.