ORACLE 语句

最近接触数据上传,从前辈汲取经验,以及自己写的一些sql语句,方便以后统计数据,以及进行一些修改操作。

1--查询各部门数据量

--注意:结束日期设置为当前日期的下一天

select e.name, zz.sumCount

  from (select z.dept_id, sum(count) as sumCount

          from (select t.dept_id,

                       t.contents,

                       sql_excute('select count(*) from ' || t.table_name ||

                                  ' where xypt_entry_date > to_date(' ||

                                  chr(39) || '2018-09-01' || chr(39) || ',' ||

                                  chr(39) || 'yyyy-MM-dd' || chr(39) ||

                                  ') and xypt_entry_date <to_date(' ||

                                  chr(39) || '2018-09-28' || chr(39) || ',' ||

                                  chr(39) || 'yyyy-MM-dd' || chr(39) || ') AND DATA_STATE=1') as count           

                  from ustcdms.dms_contents t

                  where 1=1

                 -- and t.bus_type = 'sgsxx' –查询双公示数据则取消注释,只查询双公示

                  ) z

         group by z.dept_id) zz,

       ustcdms.dms_tree e

 where zz.dept_id = e.id

 order by e.id

2--查询各目录数据量 查询某个时间段到现在的数据

 select e.name,

        t.contents,

        sql_excute('select count(*) from ' || t.table_name ||

                   ' where xypt_entry_date > to_date(' || chr(39) ||

                   '2017-07-01' || chr(39) || ',' || chr(39) ||

                   'yyyy-MM-dd' || chr(39) || ')') as count

   from ustcdms.dms_contents t, ustcdms.dms_tree e

  where t.dept_id = e.id

  order by t.dept_id;

3.查询各目录数据量 查询某个部门

 select e.name

,

        t.contents,

        sql_excute('select count(*) from ' || t.table_name ||

                   ' where xypt_entry_date >= to_date(' || chr(39) ||

                   '2018-01-01' || chr(39) || ',' || chr(39) ||

                   'yyyy-MM-dd' || chr(39) || ') and xypt_entry_date <to_date(' ||

                                  chr(39) || '2018-04-19' || chr(39) || ',' ||

                                  chr(39) || 'yyyy-MM-dd' || chr(39) || ')') as count

   from ustcdms.dms_contents t, ustcdms.dms_tree e

  where t.dept_id = e.id  and e.name

 like '市公安局'

  order by t.dept_id

 4--查询各目录数据量  查询某个时间段内

 select e.name,

        t.contents,

       sql_excute('select count(*) from ' || t.table_name ||

                                  ' where xypt_entry_date > to_date(' ||

                                  chr(39) || '2017-07-01' || chr(39) || ',' ||

                                  chr(39) || 'yyyy-MM-dd' || chr(39) ||

                                  ') and xypt_entry_date <to_date(' ||

                                  chr(39) || '2017-07-28' || chr(39) || ',' ||

                                  chr(39) || 'yyyy-MM-dd' || chr(39) || ')') as count

   from ustcdms.dms_contents t, ustcdms.dms_tree e

  where t.dept_id = e.id

  order by t.dept_id;

5.数据上传失败后,当批量修改某个时间段中的时间信息,统一修改

UPDATE T_01090059 t

 SET t.xypt_entry_date = to_date('2018-09-21 10:21:00',

                                   'yyyy-mm-dd hh24:mi:ss')

 WHERE t.name = '黄林'

   and t.xypt_entry_date >=

       to_date('2018-09-21 10:05:29', 'yyyy-mm-dd hh24:mi:ss');   (删除‘t.name=‘黄林’and’ 则是修改当前时间内所有的数据)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

6.这段时间修改IP地址,导致后台上传IP需要进行修改,IP变化太多,在数据库里面进行统一更改,但是更改的字段内容不是进行全部的更改,所以用REPLACE语句进行修改某个

字段中的部分内容。

update 表名 set 字段 = replace(字段, '要修改的内容' , '修改后的内容');

UPDATE DC_TASK_EXE_PARAM t SET t.param_value = REPLACE(t.param_value, '10.10.40.103:9996', '10.10.54.43:9996')  

10.10.40.103:9996 IP端口修改为10.10.54.43:9996

7.修改一个表的字段时间为当前时间:

 update CREDIT_UPDATE_AMOUNT t set t.update_date=sysdate;

8.当我改完这个时间字段的时候,并且已经提交,但是需要恢复这个表的数据的时候。则可以查询这个表前2个小时的数据

select * from  CREDIT_UPDATE_AMOUNT  as of timestamp sysdate-1/12

9.将查询2个前小时的数据中的某字段更新到当前表中某个字段。

update CREDIT_UPDATE_AMOUNT t1
   set t1.update_date =
       (select t2.update_date
          from (select *
                  from CREDIT_UPDATE_AMOUNT as of timestamp sysdate - 1 / 12) t2
         where t2.Id = t1.id)

 10. decode的用法

很多时候导入别的部门数据的时候,他们往往用简写数字说明的来代替统称的文字描述,这时候,decode可以将统一的数字去转换为文字。

这样就要运用到decode的用法:

select  decode(字段名称,‘字段下某个数据’,‘更换成的数据’,‘字段下某个数据,'更换成的数据') from 表名 

select  decode(t.status,‘0’,‘未提交’,‘1’,'已提交') from apply_info  t

11. distinct的用法

有段时间我要处理很多的数据,我需要批量导入,这时候就要根据某个字段中包含有哪些不同的字段,这时候,distinct就可以使用了

distinct的用法为:

 select distinct(SFZHM) FROM  QZ_TS_340000_TAB00824  

这样就可以查询某个字段下不同的数据了

原文地址:https://www.cnblogs.com/jianshuai520/p/9685909.html