博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
去重记录并做成拉链表
阅读量:5098 次
发布时间:2019-06-13

本文共 1738 字,大约阅读时间需要 5 分钟。

 

with tmp1 as (    select         t1.day        ,t1.value        ,row_number() over(order by t1.day) as rn    from (        select '2015-01-01' as day, 1 as value union all        select '2015-02-01' as day, 1 as value union all        select '2015-05-01' as day, 1 as value union all        select '2015-08-01' as day, 2 as value union all        select '2015-11-21' as day, 2 as value union all        select '2016-01-21' as day, 3 as value union all        select '2016-05-21' as day, 3 as value union all        select '2016-08-21' as day, 5 as value union all        select '2016-11-21' as day, 6 as value    ) t1) -- 将记录按日期排序生成次序列,tmp2 as (    select         case when t2.day is null then '0001-01-01' else t1.day end as day        ,t1.value        ,row_number() over(order by case when t2.day is null then '0001-01-01' else t1.day end) as rn    from tmp1 t1    left join tmp1 t2        on t1.rn = (t2.rn + 1)    where nvl(t1.value,'') <> nvl(t2.value,'')    order by day) -- 去重后并再次排序的数据,逻辑:按序列+1进行自关联,并把关联后两个值相等的记录剔除,然后再次按时间排序,生成新的次序列select     t1.day                                as start_day    ,nvl(date_add(t2.day,-1),'2999-12-31') as end_day    ,t1.value                              as change_valuefrom tmp2 t1left join tmp2 t2    on t1.rn = (t2.rn - 1);

 

+-------------+-------------+---------------+--+|  start_day  |   end_day   | change_value  |+-------------+-------------+---------------+--+| 0001-01-01  | 2015-07-31  | 1             || 2015-08-01  | 2016-01-20  | 2             || 2016-01-21  | 2016-08-20  | 3             || 2016-08-21  | 2016-11-20  | 5             || 2016-11-21  | 2999-12-31  | 6             |+-------------+-------------+---------------+--+

 

转载于:https://www.cnblogs.com/chenzechao/p/10196021.html

你可能感兴趣的文章
《算法导论》CLRS算法C++实现(一)P11 插入排序
查看>>
ZROI2018暑期集训B班训练赛#1解题报告
查看>>
ListIterator
查看>>
HDU 2855 Fibonacci Check-up 矩阵
查看>>
Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题
查看>>
scrapy爬取数据的基本流程及url地址拼接
查看>>
Python 生产者和消费者模型
查看>>
GLSL中 Billboard和Point的顶点空间变换
查看>>
Fiddler基本用法:手机抓包
查看>>
poj 1328 Radar Installation 排序贪心
查看>>
数组与字符串 1.6
查看>>
信用卡还款项目(同事封装的ajax)
查看>>
java基本概念
查看>>
Struts2学习笔记(六) 结果(Result)(上)
查看>>
ajax提交写法
查看>>
Java编程语言基础 第三章 if嵌套分支用法
查看>>
判断质数的方法
查看>>
安全和共享设置
查看>>
树链剖分
查看>>
python常用模块(一)
查看>>