oracle sql随记

字符串分割 并 行专列

方式1 
select REGEXP_SUBSTR('01#02#03#04', '[^#]+', 1, rownum) as newport 
  from dual connect by rownum <= REGEXP_COUNT('01#02#03#04', '[^#]+');

方式2
 select aa.name
    from (with a as (select '常规检查,主动检查,专项检查,其他检查' name
    from dual)
    select regexp_substr(name, '[^,]+', 1, rownum) name
    from a
  connect by rownum <= REGEXP_COUNT(name, '[^,]+')) aa;

获取当前时间所在周的起始时间

select trunc(sysdate,'WW')  from dual;

当日分段

/*3 为每3个小时为一次分割,一天分割为8次 24/3=8 */
with tempTable as
 (
   select trunc(sysdate) + 60 * (rownum - 1) * 3 / 1440 startDate,
      trunc(sysdate) + 60 * (rownum) * 3 / 1440 endDate
     from dual
   connect by level <= 8
)
 select * from temptable;

往前推15天

  select trunc(sysdate - 1- rownum) startDate,trunc(sysdate - rownum) endDate ,rownum from dual connect by level <= 15;

往前推7个周

   select trunc(sysdate - (to_number(to_char(sysdate - 1, 'd')) - 1) -
     (rownum - 1) * 7 ) as startDate,
      trunc(sysdate + (7 - to_number(to_char(sysdate - 1, 'd'))) -
     (rownum - 1) * 7 )as endDate,
     to_number(to_char(sysdate, 'iw')) - rownum + 1 as weekIndex
from dual
connect by level <= 7;

--先闭后开
select 
  trunc(sysdate - (to_number(to_char(sysdate - 1, 'd')) - 1) -
 (rownum - 1) * 7 ) as startDate,
 trunc(sysdate + (7 - to_number(to_char(sysdate - 1, 'd'))) -
 (rownum - 1) * 7+1) as endDate,
 to_number(to_char(sysdate, 'iw')) - rownum + 1 as weekIndex
  from dual
connect by level<= 7;

往前推15个月

 select 
    add_months(trunc(sysdate,'mm'),0-rownum),
    add_months(trunc(sysdate,'mm'),0-(rownum-1)) -1
  from dual connect by rownum<=15

 select add_months(trunc(sysdate, 'mm'), 1 - rownum) startDate,
        add_months(trunc(sysdate, 'mm'), 2 - rownum) endDate,
        rownum rnIndex
   from dual connect by rownum <=15;

往前推7年

 select  trunc(trunc(sysdate, 'y')-365*(rownum -1) ,'y') startDate,
        trunc(trunc(sysdate, 'y')-365*(rownum-2) ,'y')  endDate,
        rownum rnIndex
   from dual
 connect by rownum <= 7;

统计求和(总计)

select d.dm,
   decode(grouping(d.dm),1,'合计',max(d.mc)) mc,
  nvl(sum(p2.product), 0) "productTotal"
from t_dic_pcs d
left join (  select '1' comp_dm,1 product from dual  ) p2  on p2.comp_dm = d.dm
 where d.fjdm = '320582' 
 group by rollup (d.dm)
 order by 1;

先查看数据文件存放路径,统一路径

select * from dba_data_files;
比如路径为F:\app\oradata\xxx.dbf
把路径中的xxx.dbf换成jnga_01.dbf 然后填入下面datafile ''的引号中

创建表空间

create tablespace jnga
datafile ''
size 50m
autoextend on next 20m
maxsize unlimited;
--创建用户
create user jnga identified by jnga
default tablespace jnga;
--用户授权
grant connect,resource,unlimited tablespace,dba to jnga;

数据泵导入

imp jnga/[email protected]/orcl file =d:\t_dev_loc06.dmp log=d:\t_dev_loc06.log fromuser=jnga touser=jnga ignore=y

获取所有叶子节点

select t.code,t.name,level lvl,connect_by_isleaf from tableA  t
where connect_by_isleaf =1
start with t.code='xxxx'
connect by prior t.id=t.parent_id;

select t.code,t.name,level lvl,connect_by_isleaf from tableA  t
start with t.code='xxxx' connect by prior t.id=t.parent_id;

select t.code,t.name,level lvl,connect_by_isleaf from tableA  t
start with t.code='xxxx' connect by prior t.parent_id=t.id;

你可能感兴趣的:(oracle sql随记)