Orcal常用查詢例項集合

2022-02-17 00:43:06 字數 2500 閱讀 4798

本文的初衷主要是記錄工作中碰到的一些查詢例項,便於後續的工作參考從而提高效率。

一、a表拼接b表的資料,a、b兩個表字段相同,當b表有資料時用b表的,否則用a表的。區分粒度為業務日期。

select

z.fundid,

z.busidate,

z.cloumn1,

z.cloumn2

from

tablea z

where z.fundid in

(fundids)

and z.busidate between begindate and

enddate

and z.fundid || z.busidate notin(

select t.fundid || t.busidate as

unioncode

from

tableab t

group

by t.fundid ||

t.busidate)

union

select

t.fundid,

t.busidate,

'89'||

t.cloumn1,

t.cloumn2

from

tableab t

where t.fundid in

(fundids)

and t.busidate between begindate and enddate

二、orcal將多條查詢記錄拼接成一條記錄

select listagg(欄位名,'

,') within group(order

by 欄位名);

例如,查詢乙個表有3條記錄,欄位a的值分別是a、b、c。那麼使用 listagg 後將返回一條記錄a,b,c

select listagg(fundid || fundname || '

(' || alternativelevel || '

) ') within group(order by fundid,fundname,alternativelevel) from zt_alternativefundinfo where fundbankid = z.fundbankid and sysfundid = k.fundid and alternativelevel not in ('

b','

c')

三、orcal查詢分割字串

這個跟上面的相反,當乙個字段儲存是json格式或是以逗號分開的多個id,對應關聯表的多條資料。我們希望用in 來查詢,返回多條記錄,因此需要分割這個欄位的值。

regexp_substr(string, pattern, position, occurrence, modifier)

__srcstr :需要進行正則處理的字串

__pattern :進行匹配的正規表示式

__position :起始位置,從第幾個字元開始正規表示式匹配(預設為1)

__occurrence :標識第幾個匹配組,預設為1

__modifier :模式('i

'不區分大小寫進行檢索;'

c'區分大小寫進行檢索。預設為'

c'。)

例如下面的將 '123'拆分成 1、2、3共三條資料

select regexp_substr ('

1,2,3

', '

[^,]+

', 1,rownum) from dual connect by rownum<=length ('

1,2,3

') - length (regexp_replace('

1,2,3

', '

,', ''))+

1;

四、not in優化

首先外表大內錶小用in,外表小內表大則用exists

1、對於not exists查詢,內錶存在空值對查詢結果沒有影響;對於not in查詢,內錶存在空值將導致最終的查詢結果為空。

2、對於not exists查詢,外表存在空值,存在空值的那條記錄最終會輸出;對於not in查詢,外表存在空值,存在空值的那條記錄最終將被過濾,其他資料不受影響。

使用 not in 查詢乙個結果980萬條的sql用時3.918s,使用鍊錶查詢則用時3.622s多,測試了很多次鍊錶確實比not in快一點

-- not in

3.918s

select *from tablea

where .. .

and id not

in(select id from tableb);

--left 3.622s

select a.*from tablea a

left join tableb

on a.id =b.id

where .. .

and a.id is

null;

orcal模糊查詢

原來的 if test key null and key and key 修改的 if test key null and key and key like concat concat 案例 if test dataname null and dataname and data name like ...

orcal 常用命令與使用者管理

orcal常用命令 使用者加鎖 alter user scott account lock commit 必加 使用者解鎖 修改使用者密碼 alter user scott identified by 修改的密碼 使用者的管理 使用者的建立 刪除 修改 新建立的使用者沒有任何許可權,包括連線資料庫 ...

mysql通過集合查詢 連線查詢與集合查詢

4.外連線查詢 在前面講述的連線操作中,返回的結果都是滿足連線條件的記錄。有些時候,開發人員或者使用者對於不滿足連線條件的部分記錄也感興趣,這個時候就需要使用外連線查詢。外連線查詢不僅可以返回滿足連線條件的記錄,對於乙個資料表中在另乙個資料表中不匹配的記錄也可以返回。外連線查詢主要包括三種 左外連線...