Oracle Sql使用記錄

2021-08-20 09:42:44 字數 3159 閱讀 9739

oracle當前時間 及轉換

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') now,

to_char(sysdate-interval '1' year, 'yyyy-mm-dd hh24') year1,   -- 1 year ago

to_char(sysdate-10, 'yyyy-mm-dd hh24') day10,                       -- 10 day ago

to_char(sysdate-interval '1' day, 'yyyy-mm-dd hh24') day1,

to_char(sysdate-10 / 24, 'hh24:mi:ss') hour10,

to_char(sysdate-interval '1' hour, 'hh24:mi:ss') hour1,

to_char(sysdate-10 / (24*60), 'hh24:mi:ss') minute10,

to_char(sysdate-interval '1' minute, 'hh24:mi:ss') minute1,

to_char(sysdate-interval '1' second, 'hh24:mi:ss') second1

from dual;

---左連線

select t1.a_id, t1.a.name from tablea t1, tableb t2 where t1.a_id = t2.b_id(+);

--with as語句使用

with id2t as(

select distinct id, type from tableroot

id2n as(

select id, name from user

select a.id, id2n.name, id2t.type, money 

from tablemoney a, id2t, id2n

where a.id=id2t.id and a.id=id2n.id

--動態字段

with cols as

( select wm_concat('t.'||col_name) col

from ( select upper(column_name) col_name from all_tab_cols 

where table_name = upper('p_a_users_v') and owner=upper('own_a') 

and column_name not in ('id', 'date')

select 'select t.date, t.id, '||cols.col||' from p_a_users_v where date= to_date('

'$flag'

','

'mm-dd-yyyy'

')  ' from cols;

--其中 || 符號是字串連線符

--wm_concat函式可以將多個行連線為乙個字串並以逗號分隔

--字串中出現單引號時,多乙個單引號用於轉義

-- 字串轉行

with tmp as (    select 'name1, name2, name3, name4, id1, id2' colnamesstr from dual

),cols as (    select regexp_substr(colnamesstr, '[^,]+', 1, rownum) colname from tmp 

connect by rownum--行轉列******************************=

month    tid      amount

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

1        1        1000

2        1        1500

3        1        2000

1        2        999

2        2        1200

3        2        2500

1        3        888

2        3        1800

3        3        2100

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

select * from (

select month, tid, amount

from all_sales

where year = 2003

)pivot(

sum(amount) for month in (1 as jan, 2 as feb, 3 as mar, 4 as apr)

)order by tid;

-- 列轉行

with alldata as (

select id, code1, code2, code3, name1, name2, name3,

nvl( regexp_substr(codedesc, '[^,]+', 1, 1), 'unknow') desc1,

nvl( regexp_substr(codedesc, '[^,]+', 1, 2), 'unknow') desc2,

nvl( regexp_substr(codedesc, '[^,]+', 1, 3), 'unknow') desc3

from own.table1

)select * from (

select distinct id, code, name, codedesc

from alldata unpivot( (code, codedesc, name) for dd in (

(code1, desc1, name1), 

(code2, desc2, name2), 

(code3, desc3, name3), ))

where code is not null

)t order by code,name

-- pivot子句是oracle database 11g

Oracle Sql相關記錄

1.批量增加資料 insert into fasp t carolemenu guid,roleguid,menuguid select sys guid guid,43b3bf934c324381892fe59f3d244629 from fasp t carole where length pr...

oracle sql 語句 記錄備忘

1.複製表結構及其資料 create table table name new as select from table name old 2.只複製表結構 create table table name new as select from table name old where 1 2 3.只...

oracle sql 刪除重覆記錄

oracle中刪除重覆記錄,有好幾種方法。第一種 把原來表中的資料都轉存到乙個臨時表中,用distinct查詢出要存到臨時表去的資料。create table temp table as select distinct from table 不過這種方法只適用於表中每一列都相同的情況。第二種 利用o...