关于数字

1.生成累乘积

  select empno,ename,sal,exp(sum(ln(sal)) over(order by sal,empno)) as running_prod from emp where deptno = 10

 

2.计算累计差

  select ename,sal,sum(case when rn = 1 then sal else -sal end) over(order by sal) from (
    select ename,sal,row_number() over(order by sal) rn from emp where deptno = 10
    )

 

3.计算模式(查找列中出现次数最多的值)

   ①select sal,kn from(
    select sal,row_number() over(order by rn desc) kn from(
      select deptno,sal,count(sal) rn from emp where deptno = 20 group by deptno,sal
    )
  ) where kn = 1

 

 ② select max(sal) keep(dense_rank first order by rn desc) sal from(
      select sal,count(sal) rn from emp where deptno = 20 group by sal
     )

 

4.取中间值

    select median(sal) from emp

 

5.求总和的百分比

   select (s20/total)*100 pct from(
     select distinct deptno,sum(sal) over() total,sum(sal) over(partition by deptno order by deptno) s20 from emp
   ) where deptno = 10  

 

6.对可空列做聚集

  select avg(coalesce(comm,0)) avg_comm from emp

 

7.求不包含最大值和最小值的均值

  select avg(sal) from (
    select sal,max(sal) over() maxvalue,min(sal) over() minvalue from emp where deptno = 20
  ) where sal not in (maxvalue,minvalue)

 

8.更改累计和中的值

  with temp as(
select 1 id,100 amt,'PR' trx from dual
union all
select 2 id,100 amt,'PR' trx from dual
union all
select 3 id,50 amt,'PY' trx from dual
union all
select 4 id,100 amt,'PR' trx from dual
union all
select 5 id,200 amt,'PY' trx from dual
union all
select 6 id,50 amt,'PY' trx from dual
)
select case when trx = 'PR' then 'PURCHASE' else 'PAYMENT' end trx_type,
       amt,
       sum(case when trx = 'PY' then -amt else amt end) over(order by id) balance
from temp 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值