sql - 如何合并具有相同列的记录并扩展时间范围?
问题描述
如何合并具有相同列的记录并扩展时间范围?
示例表:
id | date_from | date_to | param1 | param2 | |
----|-----------|---------|--------|---------|-----|---
1 | 2009 | 2010 | 'A' | | 'A' |
1 | 2009 | 2010 | 'A' | 'A' | |
1 | 2011 | 2012 | 'A' | 'A' | |
1 | 2013 | 2014 | 'B' | 'B' | |
1 | 2015 | 2016 | 'A' | 'A' | |
1 | 2017 | 2018 | 'A' | 'A' | |
应该是这样的:
id | date_from | date_to | param1 | param2
----|-----------|---------|--------|---------
1 | 2009 | 2012 | 'A' | 'A'
1 | 2013 | 2014 | 'B' | 'B'
1 | 2015 | 2018 | 'A' | 'A'
我尝试使用窗口函数,但我不知道下一步该做什么
SELECT ROW_NUMBER() OVER(partition BY id ORDER BY date_from ASC) AS step,
dense_rank() OVER(partition BY id ORDER BY param1, param2) AS rnk,
*
FROM Table
解决方案
您可以使用lag()
& 查找分组 & 进行聚合:
select id, min(date_from) as date_from, max(date_to) as date_to, param1, param2
from (select t.*,
sum(case when (date_from - prev_to) = 1 then 0 else 1 end) over (partition by id, param1, param2 order by date_from) as grp
from (select t.*,
lag(date_to) over (partition by id, param1, param2 order by date_from) as prev_to
from table t
) t
) t
group by id, param1, param2, grp
order by id, date_from;
推荐阅读
- netbeans - 我想更改 Netbeans 中子菜单和文件资源管理器的字体大小
- vba - 使用接收日期(减去 1 个日历日)自动保存 Outlook 附件
- spring-boot - Spring Boot - 没有数据库调用的 JWT 身份验证
- r - h2o target_encode_apply kfold 生成重复项
- c# - StackExchange.Redis.RedisTimeoutException - 由于 qs 人们如何解决它?
- rest - 以编程方式定义 @DeclareRoles 注释
- typescript - 在 Typescript 中创建使用与超集类型相同的道具的更复杂类型时,如何避免代码重复?
- java - 为什么找不到这些符号?
- amazon-web-services - 无法在需要基本身份验证的页面上使用 AWS S3 对象
- html - CSS 在 URL 中使用波浪号作为具有未知完整 URL 的文件夹名称的一部分