oracle 保留 case when 上一次的值 在线等。。
本帖最后由 New_sara 于 2012-11-20 14:06:49 编辑
问题描述:
JAVA程序版
大家都知道全局变量把,就类似于下面这种。
也就是说,如何在case when 里面 保存上一次 case when 的值。
换句话就是,如何用纯SQL实现全局变量的功能。
(PS:PL/SQL 是可以实现的拉,但不允许用~~哎。。)
想用ORACLE的纯SQL实现这样的功能,也不知道能不能实现,有兴趣的大家一起讨论下啦。。
[最优解释]
with data as
(
select 'a' user_name, '2' flg, '0' code from dual union all
select 'b', '1', 'c1' from dual union all
select 'c', '2', '0' from dual union all
select 'd', '2', '0' from dual union all
select 'e', '1', 'c2' from dual union all
select 'f', '2', '0' from dual union all
select 'g', '2', '0' from dual union all
select 'h', '1', 'c3' from dual union all
select 'i', '2', '0' from dual
)
select user_name, flg, code
from (select a.user_name,
a.flg,
nvl(b.code, ' ') code,
row_number() over(partition by a.user_name order by b.user_name desc) rn
from data a, (select * from data where flg = 1) b
where a.user_name >= b.user_name(+))
where rn = 1
[其他解释]
该回复于2012-11-20 11:43:06被管理员删除
[其他解释]
有没有人看见阿。。。
[其他解释]
感觉应该可以。
[其他解释]
这个表中是否有标示顺序的字段啊?
[其他解释]
如果需要的话,可以手动给这个表加个序列。。
[其他解释]
一个窗口函数搞定的事,用什么序列。
userName flg code decode(code,0,0,1) sum(decode(code,0,0,1)) over(order by rowid)
a 2 0 0 0
b 1 c1 1 1
c 2 0 0 1
d 2 0 0 1
e 1 c2 1 2
f 2 0 0 2
[其他解释]
你这个投机取巧了。。。不是我想要的结果。。
我上面的0是代表任意一个字符串。。为了防止误解。。那我写清楚吧
变换前:
userName flg code
a 2 c3
b 1 c1
c 2 c4
d 2 c5
e 1 c2
f 2 c6
变换后
userName flg code
a 2 " " -- 一个space
b 1 c1
c 2 c1
d 2 c1
e 1 c2
f 2 c2
[其他解释]
胡扯一样,任意一个字符串凭什么下面3行是C1,然后2行C2。
为什么不是下面4行,下面5行?
[其他解释]
你应该把这个放在一张临时表里,用游标循环,变换的同时,插入一张临时表,从临时表里面直接去加工好的过程,写个过程吧。
