问题一:
我现在做报表查询
比如有这样的表
sale count ....
-60 3 ....
24 6 ....
50 5 ....
-20 2
40 8 ....
我要做这样的查询
select row_number()over(order by(sale/count)desc),sale/count
查询的结果成这样:
----------------------------------
1 10
2 5
3 4
4 -20
5 -10
负数排序就有问题了,请问怎么解决?
问题二:
还有能不能有一种传递变量的查询
比如把sale/count放到变量K里,那我就可以类似这样查,效率就比较高些:
select row_number()over(order by(K)desc),sale/count
如果能哪位高手能解决问题一,满份想送,当然能顺便回答二那更好。
------解决方案--------------------
负数排序没有问题啊
> select row_number() over(order by sale/cnt desc) as sort, sale/cnt
2 from (
3 select -60 as sale,3 as cnt from dual union all
4 select 24 as sale,6 as cnt from dual union all
5 select 50 as sale,5 as cnt from dual union all
6 select -20 as sale,2 as cnt from dual union all
7 select 40 as sale,8 as cnt from dual);
SORT SALE/CNT
---------- ----------
1 10
2 5
3 4
4 -10
5 -20
第二个问题看不懂.
------解决方案--------------------
可以使用存储过程和动态sql来实现
create or replace package sp
is
type cur is ref cursor;
procedure query( k in varchar2,Rst out cur);
end sp;
/
create or replace package body sp
is
procedure query(k in varchar2,Rst out cur)
is
sqlstr varchar2(4000);
begin
K:= 'sale/count ';
sqlstr:= 'select row_number()over(order by( '||K|| ')desc),sale/count ';
open Rst for sqlstr;
end query;
end sp;
------解决方案--------------------
select row_number()over(order by(sale/count)desc),sale/count from T_ROWNUMBER
在我电脑上的结果:
1 10
2 5
3 4
4 -10
5 -20
我二个字段都是NUMBER
你二个字段是什么数据类型
------解决方案--------------------
关于问题2,要看你的开发语言的语法,比如java的话你只要select出来自然会获得resulteset,如果是pb两层可以直接执行select column_1 into :var_1,column_2 into :var_2 from table_name using sqllca 的方式执行(前提是只一行,多行要用游标)