-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blog update
- Loading branch information
Showing
17 changed files
with
1,382 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: SQL生成连续列表 | ||
author: xxzuo | ||
tags: | ||
- doris | ||
- sql | ||
- hive | ||
categories: | ||
- SQL相关 | ||
date: 2024-03-21 20:50:33 | ||
--- | ||
有时候 需要根据 给定的 开始数据 和 结束数据,生成一段连续序列 | ||
比如 给定 开始日期 和 结束日期 生成 连续日期列表。 | ||
又或者 给定 开始数字 和 结束数字 生成连续数字列表 | ||
就像这样 | ||
``` | ||
start_date: 2024-01-01 | ||
end_date: 2024-01-10 | ||
res: | ||
2024-01-01 | ||
2024-01-02 | ||
2024-01-03 | ||
2024-01-04 | ||
2024-01-05 | ||
2024-01-06 | ||
2024-01-07 | ||
2024-01-08 | ||
2024-01-09 | ||
2024-01-10 | ||
``` | ||
|
||
这里 一般只需要处理三个地方 | ||
- 计算 开始值 和 结束值 的 差值 | ||
- 根据 差值 生成连续序列 | ||
- 把 开始值 加上 连续序列的值 | ||
|
||
这里 以 doris 和 hive 来举例 | ||
### Doris生成连续日期 | ||
|
||
生成 `2024-01-01` 和 `2024-01-10`的连续日期序列 | ||
|
||
- 使用 `datediff`计算差值 | ||
[DATEDIFF - Apache Doris](https://doris.apache.org/zh-CN/docs/sql-manual/sql-functions/date-time-functions/datediff/) | ||
``` | ||
select datediff('2024-01-10', '2024-01-01') | ||
``` | ||
|
||
- 利用 explode_number 生成连续序列 | ||
[EXPLODE_NUMBERS - Apache Doris](https://doris.apache.org/zh-CN/docs/sql-manual/sql-functions/table-functions/explode-numbers/) | ||
``` | ||
select e1 | ||
from (select '2024-01-01' k1) as t lateral view explode_numbers(datediff('2024-01-10', '2024-01-01')) tmp1 as e1; | ||
``` | ||
|
||
- 开始值 加上 连续序列 | ||
[DAYS_ADD - Apache Doris](https://doris.apache.org/zh-CN/docs/sql-manual/sql-functions/date-time-functions/days-add/) | ||
``` | ||
select e1, k1, to_date(days_add(k1, e1)) | ||
from (select '2024-01-01' k1) as t lateral view explode_numbers(datediff('2024-01-10', '2024-01-01') + 1) tmp1 as e1; | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: 开窗函数-排名 | ||
author: xxzuo | ||
tags: | ||
- sql | ||
categories: | ||
- SQL相关 | ||
date: 2024-02-03 23:42:33 | ||
--- | ||
|
||
排名主要利用三种函数 | ||
|
||
- **rank() 排序相同时重复,总数不会减少** | ||
|
||
- **dense_rank() 排序相同时重复,总数减少** | ||
|
||
- **row_number() 排序不重复,总数不变** | ||
|
||
比如对如下表根据分数进行降序排名 | ||
|
||
|id|grade| | ||
|---|---| | ||
|1|97| | ||
|2|96| | ||
|3|95| | ||
|4|95| | ||
|5|98| | ||
|6|94| | ||
|
||
**rank()** | ||
|
||
|id|grade|rank| | ||
|---|---|---| | ||
|5|98|1| | ||
|1|97|2| | ||
|2|96|3| | ||
|3|95|4| | ||
|4|95|4| | ||
|6|94|6| | ||
|
||
```sql | ||
select id, grade, rank() over(order by grade) as rank from data_table | ||
``` | ||
|
||
|
||
**dense_rank()** | ||
|
||
|id|grade|rank| | ||
|---|---|---| | ||
|5|98|1| | ||
|1|97|2| | ||
|2|96|3| | ||
|3|95|4| | ||
|4|95|4| | ||
|6|94|5| | ||
|
||
```sql | ||
select id, grade, dense_rank() over(order by grade) as rank from data_table | ||
``` | ||
|
||
|
||
**row_number()** | ||
|
||
|id|grade|rank| | ||
|---|---|---| | ||
|5|98|1| | ||
|1|97|2| | ||
|2|96|3| | ||
|3|95|4| | ||
|4|95|5| | ||
|6|94|6| | ||
|
||
```sql | ||
select id, grade, row_number() over(order by grade) as rank from data_table | ||
``` |
Oops, something went wrong.