oracle中decode和case的使用例子
2007-03-30 16:09
676 查看
decode是很不错的条件函数,但是它却不能使用范围来进行条件区分,而case则刚好弥补了这一缺点,以下是二者的例子比较:
select pay_name,subgate,
sum(decode(floor(order_amount/10000),0,order_amount,0)) as one_sum,
sum(decode(floor(order_amount/10000),0,1,0)) as one_count,
sum(decode(floor(order_amount/10000),1,order_amount,0)) as one_two_sum,
sum(decode(floor(order_amount/10000),1,1,0)) as one_two_count,
sum(decode(floor(order_amount/10000),2,order_amount,0)) as two_three_sum,
sum(decode(floor(order_amount/10000),2,1,0)) as two_three_count,
sum(decode(floor(order_amount/10000),3,order_amount,4,order_amount,0)) as three_five_sum,
sum(decode(floor(order_amount/10000),3,1,4,1,0)) as three_five_count,
sum(decode(floor(order_amount/10000),5,order_amount,6,order_amount,7,order_amount,8,order_amount,9,order_amount,0))
as five_ten_sum,
sum(decode(floor(order_amount/10000),5,1,6,1,7,1,8,1,9,1,0)) as five_ten_count,
sum(decode(floor(order_amount/100000),0,0,order_amount)) as ten_sum,
sum(decode(floor(order_amount/100000),0,0,1)) as ten_count
from order_tab
where order_date>=to_date('20061201','yyyymmdd')
and order_date<to_date('20070201','yyyymmdd')
and order_status=30
and lower(pay_name) in ('chinapay','cbj','abc','icbc','cbc','cmb')
group by pay_name,subgate
order by pay_name,subgate;
select sum(case when count_01 <=10 then 1 else 0 end) as le10,
sum(case when count_01 >10 and count_01 <=20 then 1 else 0 end) as le20,
sum(case when count_01 >20 and count_01 <=50 then 1 else 0 end) as le50,
sum(case when count_01 >50 and count_01 <=100 then 1 else 0 end) as le100,
sum(case when count_01 >100 and count_01 <=200 then 1 else 0 end) as le200,
sum(case when count_01 >200 and count_01 <=500 then 1 else 0 end) as le500,
sum(case when count_01 >500 and count_01 <=1000 then 1 else 0 end) as le1000,
sum(case when count_01 >1000 then 1 else 0 end) as g1000
from (select ssn,sum(t.changepoints + t.fpoints) count_01
from pay_detail_new_200701 t
where serviceip not in ('charge','ptransfer')
group by t.ssn);
感谢lisa提供的case例子!
select pay_name,subgate,
sum(decode(floor(order_amount/10000),0,order_amount,0)) as one_sum,
sum(decode(floor(order_amount/10000),0,1,0)) as one_count,
sum(decode(floor(order_amount/10000),1,order_amount,0)) as one_two_sum,
sum(decode(floor(order_amount/10000),1,1,0)) as one_two_count,
sum(decode(floor(order_amount/10000),2,order_amount,0)) as two_three_sum,
sum(decode(floor(order_amount/10000),2,1,0)) as two_three_count,
sum(decode(floor(order_amount/10000),3,order_amount,4,order_amount,0)) as three_five_sum,
sum(decode(floor(order_amount/10000),3,1,4,1,0)) as three_five_count,
sum(decode(floor(order_amount/10000),5,order_amount,6,order_amount,7,order_amount,8,order_amount,9,order_amount,0))
as five_ten_sum,
sum(decode(floor(order_amount/10000),5,1,6,1,7,1,8,1,9,1,0)) as five_ten_count,
sum(decode(floor(order_amount/100000),0,0,order_amount)) as ten_sum,
sum(decode(floor(order_amount/100000),0,0,1)) as ten_count
from order_tab
where order_date>=to_date('20061201','yyyymmdd')
and order_date<to_date('20070201','yyyymmdd')
and order_status=30
and lower(pay_name) in ('chinapay','cbj','abc','icbc','cbc','cmb')
group by pay_name,subgate
order by pay_name,subgate;
select sum(case when count_01 <=10 then 1 else 0 end) as le10,
sum(case when count_01 >10 and count_01 <=20 then 1 else 0 end) as le20,
sum(case when count_01 >20 and count_01 <=50 then 1 else 0 end) as le50,
sum(case when count_01 >50 and count_01 <=100 then 1 else 0 end) as le100,
sum(case when count_01 >100 and count_01 <=200 then 1 else 0 end) as le200,
sum(case when count_01 >200 and count_01 <=500 then 1 else 0 end) as le500,
sum(case when count_01 >500 and count_01 <=1000 then 1 else 0 end) as le1000,
sum(case when count_01 >1000 then 1 else 0 end) as g1000
from (select ssn,sum(t.changepoints + t.fpoints) count_01
from pay_detail_new_200701 t
where serviceip not in ('charge','ptransfer')
group by t.ssn);
感谢lisa提供的case例子!
相关文章推荐
- ORACLE中使用decode实现行转列以及使用case通用的行转列
- oracle case when的使用方法
- orcale单行函数之Case, decode在 SQL 语句中使用IF-THEN-ELSE 逻辑
- oracle里面如何写case语句及decode and sign 函数用法
- Oracle decode使用
- [Oracle] SQL*Loader 详细使用教程(5)- 典型例子
- 按月统计订单数量报表SQL语句(CASE 和DECODE的使用)
- Oracle中Decode()函数使用技巧
- oracle 存储过程(其中使用了游标)例子
- Oracle nvl、nvl2、nullif、decode、case函数详解
- [转]Oracle中使用Rownum分页详细例子
- ORACLE分析函数over(partition by...)中使用case when达到多重分组的效果
- 在存储过程中演示使用case的例子(二)
- Oracle select 中case 的使用以及使用decode替换case
- Oracle 正则表达式函数-REGEXP_SUBSTR 使用例子
- Oracle中使用Rownum分页详细例子
- Oracle 行转列总结 Case When,Decode,PIVOT 三种方式
- Oracle case when decode and sequence
- Oracle decode使用实例
- Oracle中Decode()函数的使用