我正在撰写一份报告(为了简化用户方面)我想使用oracle查询将一些数据聚合在一起,最终将导入到excel中,但可以自动运行到打印机好吧,所以处理需要在查询本身完成。
我正在使用oracle,版本10.2.0.5.0(从v $ instance中选择版本)。
好的,我正在使用此查询;
SELECT substr(arg_string,1,4) node,
substr(numtodsinterval(end_time-start_time,'DAY'), 12, 8) dur,
row_number() over (order by substr(arg_string,1,4),
start_time) rn
FROM pro.program_status
where prog_name like ('%v8x-6%')
and start_time > sysdate - 10;
返回类似于以下内容的数据集;
NODE DUR RN
---- -------- ----------
s002 00:25:40 1
s002 00:27:21 2
s002 00:03:20 3
s002 00:27:44 4
...
s004 00:21:29 22
s004 00:22:19 23
s004 00:05:13 24
s004 00:22:47 25
...
s005 00:03:13 42
s005 00:00:46 43
s005 00:02:01 44
s005 00:00:45 45
...
s152 00:01:13 1840
我想要的输出如下;
NODE DUR1 DUR2 DUR3
---- -------- -------- ----------
s002 00:25:40 00:27:21 00:03:20
s004 00:21:29 00:22:19 00:05:13
s005 00:03:13 00:00:46 00:02:01
and so on
我尝试了以下查询;
SELECT node,
max( CASE WHEN rn = 1 THEN dur ELSE NULL END ) dur1,
max( CASE WHEN rn = 2 THEN dur ELSE NULL END ) dur2,
max( CASE WHEN rn = 3 THEN dur ELSE NULL END ) dur3
FROM (SELECT substr(arg_string,1,4) node,
substr(numtodsinterval(end_time-start_time,'DAY'), 12, 8) dur,
row_number() over (order by substr(arg_string,1,4),
start_time) rn
FROM pro.program_status
where prog_name like ('%v8x-6%')
and start_time > sysdate - 10)
group by node
/
然而,这会返回以下结果,因为只有's002'标记为1,2和3
NODE DUR1 DUR2 DUR3
---- -------- -------- --------
s002 00:25:40 00:27:21 00:03:20
s004
s005
s009
and so on
我确信我错过了一些明显的东西,但是在搜索了这个网站和其他许多网站后,我似乎无法正常工作。
更详细一点,我不知道每个节点出现的次数或每天会有多少节点。
提前致谢。 汤姆
答案 0 :(得分:2)
尝试:
SELECT node,
max( CASE WHEN rn = 1 THEN dur ELSE NULL END ) dur1,
max( CASE WHEN rn = 2 THEN dur ELSE NULL END ) dur2,
max( CASE WHEN rn = 3 THEN dur ELSE NULL END ) dur3
FROM (SELECT substr(arg_string,1,4) node,
substr(numtodsinterval(end_time-start_time,'DAY'), 12, 8) dur,
row_number() over (partition by substr(arg_string,1,4)
order by substr(arg_string,1,4), start_time) rn
FROM pro.program_status
where prog_name like ('%v8x-6%')
and start_time > sysdate - 10)
group by node