Oracle query skills and experience summary

Apply what you have learned, welcome to reprint, contact QQ:289325414 for more

 

Some of my own commonly used experience writing

table of Contents

1. Data condition filtering

Second, the divisor is zero

Three, get the consecutive days of a week

Five, instr()

Six, judge whether the field is empty

Seven, statistical SQL, add judgment in the function count(), sum(), count() de-duplication usage 

Eight, update method:

Nine, user unlock

Ten, table unlock

11. Lookup table name

Twelve, query whether there is data in the partition, and get the partition with data


1. Data condition filtering

select a from table where a=b field a do not use functions to convert

to_date, to_char, substr, will greatly slow down query efficiency

Second, the divisor is zero

The divisor is zero, and the error is avoided. Select decode(b,0,0,a/b) from dual;

 

Three, get the consecutive days of a week

SELECT to_char (to_date('2018-01-24')- LEVEL + 1, 'yyyy-mm-dd') today FROM

DUAL connect BY LEVEL <= 7;

Four, trunc

trunc function, trunc(sysdate,'DD') gets the yyyy-mm-dd standard date, removes the hour, minute and second, trunc(sysdate,'MM') gets a certain date on the first day of the month

Five, instr()

instr() function. Equivalent to like'%xxx%'

select * from test t  

where instr(t.xxx,'xx')>0  

or method usage instead

select * from test t  

where instr(‘123,321’,t.xxx)>0  

Six, judge whether the field is empty

select*from tab_i t where 1=nvl(t.col_x,1);

Not empty

select*from tab_i t where 1!=nvl(t.col_x,1);

 

Seven, statistical SQL, add judgment in the function count(), sum(), count() de-duplication usage 

with t1 as (
select '张三' as name , '语文' as calls , '89' as scare from dual
union all
select '张三' as name , '语文' as calls , '88' as scare from dual
union all
select '张三' as name , '数学' as calls , null as scare from dual
union all
select '张三' as name , '英语' as calls , '68' as scare from dual
union all
select '李四' as name , '语文' as calls , '99' as scare from dual
union all
select '李四' as name , '数学' as calls , '66' as scare from dual
union all
select '李四' as name , '英语' as calls , null as scare from dual
)
select  name,count(case when scare is not null then calls else null end) as There are several ways to write count for courses where the score is not null
,sum(case when scare is not null then 1 else 0 end) as a course with a non-empty score, there are several ways to write sum
, count(distinct case when scare is not null then calls else null end) as a course with a non-empty score There are several ways to go to reprint
from t1 group by name;

 

Eight, update method:

1. A data update B table data

update P_H_MME_QUALITY_CHECK A set A.LOWSINR0=

(SELECT B.LOWSINR0 FROM P_H_RST_XDR_COVERS B WHERE A.HOUR_ID=B.HOUR_ID

AND A.MSISDN=B.MSISDN AND A.VERSION_DATE=B.VERSION_DATE)

COMMIT;

 

Nine, user unlock

alter user username account unlock; (username refers to the database user name)

Ten, table unlock

SELECT 'alter system kill session ''' || c.sid || '' || ',' || c.serial# ||''';'

, a.object_id, a.session_id, b.object_name, c.*

FROM v$locked_object a, dba_objects b, v$session c

WHERE a.object_id = b.object_id

AND a.SESSION_ID = c.sid(+)

AND B.OBJECT_NAME = 'test_table'

ORDER BY logon_time;

 

 

11. Lookup table name

select table_name from user_tables where table_name like '%CJW%';

 

Twelve, query whether there is data in the partition, and get the partition with data

1. Analyze table test_table compute statistics for table for all indexes for all indexed columns; the
first step is usually after the partition data is stored in the database, call, call the analysis partition data, after analysis, the partition information of the table (USER_TAB_PARTITIONS) will be make it complete

2、SELECT substr(PARTITION_NAME,2) yyyymmdd FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = 'test_table' 
AND SAMPLE_SIZE IS NOT NULL

The second step is real-time query, which can quickly figure out which partitions have data

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_37203082/article/details/100194680