美文网首页
Oracle基础知识小结

Oracle基础知识小结

作者: 梦游的猫头鹰 | 来源:发表于2020-01-09 16:15 被阅读0次

1、字符串类操作

CLOB字段操作:

select * from tab1 t where DBMS_LOB.SUBSTR(t.name, 4000, 1) = '产品名'

select substr(t.idcard, 7, 8) from tab1;   --  从第7位开始,截取8位。

TT地址类变量的操作\生意贷:substr + instr

字符拼接:col1 || col2

2、时间类操作

select sysdate,add_months(sysdate,12) from dual;        --加1年 

select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual;  --加1分钟;可直接to_char(sysdate, 'HH24')

select date'2019-10-01' from dual    -- 快捷操作

month_between(t1, t2)   -- 月份差

last_day(t1)  -- 最后一天

trunc(date1)  -- 只截取时间数据到天

date'2020-03-22'  -- 快捷设置时间格式数据

-- 生成时间序列

select rownum r, last_day(add_months(trunc(sysdate), -rownum)) as enddate

from dual

connect by rownum <= 36

3、窗口函数操作

简单排序:

select t.*, row_number() over(partition by t.idcard order by t.applytime desc) as rn

from tab1 t

where t.rn = 1

偏移计算函数:lag、lead

select listagg(name, ',') within GROUP(order by name) OVER(partition by customer)

4、常用函数\表操作\系统表

nvl:填充空值。

decode:简易便捷的case when。

trunc:截取时间到日、或者当年当周第一天等。

avg:求平均(比习惯的sum/count更简洁),eg:avg(nvl(x, 0))

variance:求方差

coalesce:如为空即填充后面字段值

truncate table tab1;  --清空表

delete from tab1 where name = 'xl'; commit;

update tab1 set col1 = 1 where col2 is null

alter table tab1 drop column col1;

5、良好习惯

时间比较操作时,增加trunc,截取到日期。

比值类计算时,无特别精度要求,计算结果round保留4位小数。

占比计算时,防止分母为零,可用decode函数编码(为空或许好于-999,-999容易在python处理时误分箱),需要根据变量的业务含义决定-999还是null。

round(decode(y, 0, -999, x/y), 4) as z

6、其他

并行模型:select  /*+ parallel(4) */ t.name from table1 t

限制条数:select * from tab1 where rownum < 10

临时表操作:with tab1 as (), tab2 as () select * from tab1

同一表中两列比较:select * from tab1 t1 where t1.name1= t1.name2

left join tab2 on 1 = 1  -- 所以是全连接

相关文章

网友评论

      本文标题:Oracle基础知识小结

      本文链接:https://www.haomeiwen.com/subject/lekmnctx.html