資料庫 oracle常用sql總結 持續更新中

2021-06-21 03:42:55 字數 3448 閱讀 3389

1、將long型如1350149400000 轉換為2014-3-23 13:38:22資料函式(將long型時間轉為年月日格式)

create or replace function num_to_date(in_number number) return date is

begin

return(to_date('19700101','yyyymmdd')+ in_number/86400000+

to_number(substr(tz_offset(sessiontimezone),1,3))/24 );

end num_to_date;

2、刪除重複資料

1)、使用distinct函式,重複字段。

選出所有唯一的資料,sql如下:

select distinct 欄位1,欄位2,欄位3 from 表名;
匯入臨時表:

create table 臨時表名 as select distinct 欄位1,欄位2,欄位3 from 表名;
再從臨時表恢復。

insert into 表名 select * from 臨時表名;
2)、使用rowid

delete from 表名 where rowid not in (select min(rowid) from 表名 group by 欄位1,欄位2,欄位3);
實際業務需求

1)、資料表t(date,num),資料如左圖所示,要求用乙個sql查出右邊結果:

所用到的關鍵點,group by,sum,以及union all,sql

select t.date, sum(t.num) as num from t group by t.date

union all

select '合計' as "date", sum(t.num) as num from t;

2)、學生表如下:

自動編號學號姓名課程編號課程名稱分數

1        2005001  張三  0001      數學    69

2        2005002  李四  0001      數學    89

3        2005001  張三  0001      數學    69

刪除除了自動編號不同,其他都相同的學生冗餘資訊。     

關鍵字,group by,rowid,sql

delete 學生表 where 自動編號 not in(select min(自動編號) from 

tablename group by 學號,姓名,課程編號,課程名稱,分數);

3)、學生表student如下,列出所有課程分數都大於80的學生姓名

關鍵字還是group by,sql如下:

select name, score from 

(select name,min(score) score from student group by name)

where score > 80;

3、按分號擷取,判斷包含幾個字段

select length('a;b;c;d') - length(replace('a;b;c;d', ';')) dnum from dual t;
實際業務需求

select * from 

(select searchcode, cirelsearchcode, cirelsubtype,

length(cirelsearchcode) - length(replace(cirelsearchcode, ';')) dnum1,

length(cirelsubtype) - length(replace(cirelsubtype, ';')) dnum2

from upload_ci_full t)tt

where tt.dnum1 <> dnum2;

4、to_char與to_date(具體的時間欄位和表名都可以修改)

to_char轉換為to_date

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as curtime from dual; --日期轉化為字串   

select to_char(sysdate,'yyyy') as curyear from dual; --獲取年

select to_char(sysdate,'mm') as curmonth from dual; --獲取月

select to_char(sysdate,'dd') as curday from dual; --獲取日

select to_char(sysdate,'hh24') as curhour from dual; --獲取時

select to_char(sysdate,'mi') as curminute from dual; --獲取分

select to_char(sysdate,'ss') as cursecond from dual; --獲取秒

to_date轉為to_char

select to_date('2014-3-26 11:12:22','yyyy-mm-dd hh24:mi:ss') as curtime from dual;
5、拼接表名,擷取字段

select 'pm_raw_'||substr(ext_datasource,0,1)||'_'||kbp_class from kpi_info;
6、分頁rownum

查詢前10條記錄

select * from 表名 where rownum <= 10; -- 使用rownum <> 11 或者 rownum != 11 都可以;
查詢中間記錄,注意邊界問題。在實際需求中,關注是否需要排序。

select * from (select rownum rn, 欄位名 from 表名) where rn >= 2 and rn <= 10;
rowid是實體地址,rownum是邏輯位址,有區別。

ORACLE資料庫常用SQL

1.新增乙個表,通過另乙個表的結構和資料 create table product bak as select from product2.如果表存在 insert into product bak select from product 3.同乙個表中,將a欄位的指賦給b欄位 update pro...

Oracle資料庫常用SQL

oracle ora 00984 column not allowed here ora 00984錯誤 列在此處不允許 當資料以char的形式存在時,應加單引號,則插入資料庫就不會出現類似錯誤.oracle實現select的結果集隨機展示 select from tablename order b...

Oracle資料庫常用SQL

oracle資料庫建立例項的過程類似於sql server建立資料庫,oracle乙個例項可以對應多個表空間,乙個表空間對應乙個使用者,根據不同的使用者名稱 密碼登入不同的表空間。因此,建立表空間後,緊接著要建立使用者並為其指定表空間。並授權給該使用者,一般是connect resource dba...