有表如下:
NUM_TIME NUM
2014-05-01 01:00:00 1
2014-05-01 02:00:00 1
2014-05-01 03:00:00 2
2014-05-01 04:00:00 2
2014-05-01 05:00:00 -3
2014-05-01 06:00:00 -3
2014-05-01 07:00:00 -1
2014-05-01 08:00:00 -1
问题如下:
需要同时找出NUM列绝对值的最大和最小值以及相对应的NUM_TIME,结果NUM显示正负号。当判断NUM绝对值相同,时间取发生最早的时间。注意返回结果是形成一条记录!
显示的结果如下:
MAX_NUM_TIME MAX_NUM MIN_NUM_TIME MIN_NUM
2014-05-01 05:00:00 -3 2014-05-01 01:00:00 1
期待各位高手的精彩解答,100积分奉上~
------解决方案--------------------
写个Function 分分钟搞写
------解决方案--------------------
这个特别适合keep函数的使用场景
select min(num_time)keep(dense_rank last order by abs(num)) max_num_time,
min(num)keep(dense_rank last order by abs(num),num_time desc) max_num,
min(num_time)keep(dense_rank first order by abs(num)) min_num_time,
min(num)keep(dense_rank first order by abs(num),num_time) min_num from t;