在http://www.itpub.net/thread-1834035-1-1.html看到一个使用sign分组的用法,个人觉得不错,记录下:
问题:
想知道满足下面条件的
sale > 2000 and age > 22,
sale > 2000 and age < 22,
sale < 2000 and age > 22,
sale < 2000 and age < 22
数量及sale总和:
普通写法:使用四个union all,具体的自己写,类似下面sql:
with tmp_t as( select 1 as v_id,3000 as sale,22 as age from dual union all select 2,2100,23 from dual union all select 3,2300,21 from dual union all select 4,1000,19 from dual union all select 5,1000,23 from dual union all select 6,2600,25 from dual union all select 7,2200,18 from dual union all select 8,1500,20 from dual union all select 9,1600,35 from dual union all select 10,2000,22 from dual union all select 11,2000,21 from dual ) select sum(case when sale<2000 and age<22 then 1 else 0 end) as 小于2000且age小于22, sum(case when sale<2000 and age<22 then sale else 0 end) as sale总和, sum(case when sale<2000 and age>22 then 1 else 0 end) as 小于2000且age大于22, sum(case when sale<2000 and age>22 then sale else 0 end) as sale总和, sum(case when sale>2000 and age<22 then 1 else 0 end) as 大于2000且age小于22, sum(case when sale>2000 and age<22 then sale else 0 end) as sale总和, sum(case when sale>2000 and age>22 then 1 else 0 end) as 大于2000且age大于22, sum(case when sale>2000 and age>22 then sale else 0 end) as sale总和 from tmp_t
结果如下:
使用sign函数sql如下:
with tmp_t as( select 1 as v_id,3000 as sale,22 as age from dual union all select 2,2100,23 from dual union all select 3,2300,21 from dual union all select 4,1000,19 from dual union all select 5,1000,23 from dual union all select 6,2600,25 from dual union all select 7,2200,18 from dual union all select 8,1500,20 from dual union all select 9,1600,35 from dual union all select 10,2000,22 from dual union all select 11,2000,21 from dual ) select decode(sign(sale-2000),1,'大于2000',-1,'小于2000'),decode(sign(age-22),1,'大于22',-1,'小于22'),count(1),sum(sale) from tmp_t group by sign(sale-2000),sign(age-22) having sign(sale-2000)*sign(age-22)<>0 order by sign(sale-2000),sign(age-22)
结果如下:
全文完。
相关推荐
Oracle数据库使用分组函数来对数据进行聚集
oracle笔记分组函数,涉及组函数案例,在使用组函数的过程中注意事项,有具体的案例说明!
ORACLE crc3函数,将字符类型转换成唯一的数字类型,例如CSDN,经过crc32函数转换后,得到的值为-1016176976
oracle自定义日期函数、你值得拥有!
简单的oracle 时间比较函数,自己在网上找到并加以整理出来。
ORACLE的OVER函数使用技巧ORACLE的OVER函数使用技巧ORACLE的OVER函数使用技巧
Oracle字符相似度函数。在sql语句中直接用该函数来计算2个字符串相似度。
oracle函数大全 oracle函数大全 oracle函数大全
oracle之占比函数 oracle之占比函数 oracle之占比函数
oracle中 substring函数的使用
ORACLE字符串函数ORACLE字符串函数ORACLE字符串函数
oracle开窗函数学习技巧总结,主要讲解over函数的使用的哦
oracle 分组函数 一看就知道,包括了 最常用的 分组函数,
oracle 函数大全oracle 函数大全oracle 函数大全oracle 函数大全oracle 函数大全oracle 函数大全oracle 函数大全
Oracle SQL 内置函数大全 SQL中的单记录函数 给出整数,返回对应的字符 连接两个字符串 增加或减去月份 用于对查询到的结果进行排序输出
本博客简单介绍一下oracle分组函数之rollup的用法,rollup函数常用于分组统计,也是属于oracle分析函数的一种 环境准备 create table dept as select * from scott.dept; create table emp as select * from ...
Oracle自定义聚合函数,分析函数.
XMLTYPE_instance:XML数据类型的字段,用于保存XML ,XPath_string:操作的节点路径,例如:/info/Owner。 绝对路径与相对路径:区别在于路径字符串最前面的斜杠是否存在,有斜杠代表绝对路径,没有斜杠则是相对路径...
oracle 10g函数大全 oracle 10g函数大全 oracle 10g函数大全
oracle的分析函数over 及开窗函数