with temp as (
select '1' 标识,'2023-01-01' 日期,'a' 项目
union all select '1','2023-01-01','a'
union all select '2','2023-01-01','a'
union all select '2','2023-01-01','b'
union all select '3','2023-01-01','a'
union all select '3','2023-01-01','b'
union all select '3','2023-01-01','c'
union all select '4','2023-01-01','a'
union all select '4','2023-01-01','b'
union all select '4','2023-01-01','c'
union all select '4','2023-01-01','c'
union all select '4','2023-01-02','c'
)
select
*,
--相同并列数字
(DENSE_RANK() over(partition by 标识,日期 order by 项目)) DENSE_RANK相同并列数字,
--此位有跳跃
RANK() OVER(PARTITION BY 标识,日期 ORDER BY 项目 DESC) RANK此位有跳跃,
--分组后依旧按照顺序
(row_number() over(partition by 标识,日期 order by 项目)) row_number分组后依旧按照顺序
from
temp
order by
标识,日期,项目
查询结果图,主键为“标识和日期”,排序为“项目”

文章详细描述了一个SQL查询,使用了DENSE_RANK,RANK和ROW_NUMBER窗口函数对数据进行处理,包括相同并列数字、有跳跃的位置以及分组后的顺序,最终以标识和日期为主键,项目为排序依据。
1346

被折叠的 条评论
为什么被折叠?



