其中,requestid不固定,就是说也会有14115,14116,同时,而每个requestid也不确定会有多少行数据。求最后那列的值,就比方说,看图14414数据,有5行数据,第一行减第二行的数据为5分钟,第三行减第2行的为13分钟,以此类推,如果下一行数据的requestid与上一行的requestid不同,则用sysdate减去最后那行数据。。看图中红色部门,就是没算出来。跪求各位老大.......
实力很雄厚,呵,再给你个图,然后给你解释下。第一列,requestid,第二列:创建时间(czsj),根据不同的请求ID,看有颜色标识的部分,第一个13438为三行数据,得到第二行减去第一行的CZSJ,第三行减去第二行的时间,那么第三行,肯定就需要用sysdate减了。然后再下一个requestid,以此类推下去,每个requestid对应的行数不固定,每个requestid也不固定,图只代表一部分。有劳了。
追答这个和上边哪个图是一样的,我给你的SQL就能实现,方便的话扣我250267873
本回答被提问者采纳lead是在所有集合中用的下一行数据减去上一行,而我的那个数据必须得根据requestid来区分,所以这个不可取。
追答而我的那个数据必须得根据requestid来区分
lead(czsj) over(partition by requestid order by czsj)
其中partition by requestid 就是根据requestid来区分
可以给你个例子:
with tab as(
select 14414 requestid, sysdate - 10 czsj from dual
union all
select 14414 requestid, sysdate - 9.5 czsj from dual
union all
select 14414 requestid, sysdate - 0.3 czsj from dual
union all
select 14415 requestid, sysdate - 2.4 czsj from dual
union all
select 14415 requestid, sysdate - 0.1 czsj from dual
)
select requestid,
czsj,
floor(dif) || '天' || floor((dif - floor(dif)) * 24) || '小时' ||
mod(floor((dif - floor(dif)) * 24 * 60), 60) || '分钟'
from (select requestid,
czsj,
nvl(lead(czsj) over(partition by requestid order by czsj),
sysdate) - czsj dif
from tab)
order by 1, 2
杯具了。。全部union all,你知道有多少数据么?现在才三天,上万条数据,哥们,你这也太??
追答兄弟。。。我只是给你个例子。。。你把tab换成你的tab就行了。
另外requestid, czsj 上建索引,看看效率。
-- 给你测试个吧。。。100万数据量的:
-- table
create table test(requestid number, czsj date);
create index test_idx1 on test(requestid, czsj);
-- test data
-- 37.171'
insert into test
select mod(abs(dbms_random.random), 1000) + 10000,
sysdate - dbms_random.value * 100
from dual
connect by rownum <= 1000000;
-- get result
select requestid,
czsj,
floor(dif) || '天' || floor((dif - floor(dif)) * 24) || '小时' ||
mod(floor((dif - floor(dif)) * 24 * 60), 60) || '分钟'
from (select requestid,
czsj,
nvl(lead(czsj) over(partition by requestid order by czsj),
sysdate) - czsj dif
from test)
order by 1, 2
-- 直接在plsql中执行,返回前15行0.765s