1.
多行數據一行顯示sql:(用sum函數或max函數,當lc為1時DQSYJRZC的值) sum函數只能查字段類型為number型的,若字段值是漢字則用max函數
select nsrsbh , sum(decode(lc,1,DQSYJRZC)) JYXJRZC1,sum(decode(lc,2,DQSYJRZC)),sum(decode(lc,3,DQSYJRZC)),sum(decode
(lc,4,DQSYJRZC)),sum(decode(lc,5,DQSYJRZC)) JYXJRZC2 from sb_nd_f07 t
where nsrsbh='371325751788249' and t.skqsrq=20120101 and t.skjzrq=20121231 group by nsrsbh
2.
2.修改表名sql:
rename gsj_wsfwt_dm_swjg_ds to gsj_nsxydj_dm_swjg_ds;
3.
把空值轉換成字符串:
decode(TT.BGYY,null,'')
數據庫字符串連接:(用||)
'評定為' || tt.pdjg || '級。' || decode(TT.BGYY,null,'')
4.
把查詢一個表的數據插入另一個表:
insert into gsj_nsxydj_gy_pdlc_ls(id,slh,nsrsbh,nsrmc,wsdm,lzzt,clsj,clry)
select gsj_gy_pdlc_ls.nextval,t.slh,t.nsrsbh,t.nsrmc,t.wsdm,t.dqzt,clsj,'"+dqry.getSwry_dm()+"' from gsj_nsxydj_gy_pdlc t where
t.nsrsbh = '"+data.getNsrsbh()+"' and t.pc_dm = '"+data.getPc_dm()+"' and t.wsdm = 'pd001'"
5.
強制使用索引:
select /*+index(dj_nsrxx IDX_DJ_NSRXX_P_NSRSBH) */ * from dj_nsrxx
6.
減去(查詢一個表比另一個表多余的數據)
select d.nsrsbh
from temp_bd d
minus
select d.nsrsbh
from temp_bd2 d
7
.--查詢重復記錄
select t.nsrsbh, count(t.nsrsbh)
from cs_bd2 t
having count(t.nsrsbh) > 1
group by t.nsrsbh
8.
分析表;analyze table gsj_nsxydj_zzdf_zd compute statistics
9.截字符串:
select substr(t.nsrsbh,-9)
from dj_nsrxx t
where t.nsrsbh = '371325751788249'--------751788249
10.查詢數據的對應行數:
select rownum from zzs_xgmnsr_basedata_2012 t
11.
.復制表結構建表:
create table Sb_Cwbb_Zcfzb_2013 as select * from SB_CW_ZCFZB_2007 where 1=2;
從另一表裡抽數據
insert into table1 select *from user.table2
12
條件sql語句(case....when....then.....end):
select t.ywlx_dm,(case ywlx_dm when '0072' then '呵呵' when 'XG01' then '小規模' end) ywlx
from dm_ywcd t where t.ywlx_dm in ('0072','XG01')
13
表解鎖:
SELECT OBJECT_ID,SESSION_ID,SERIAL#,
ORACLE_USERNAME,OS_USER_NAME,S.PROCESS
FROM V$LOCKED_OBJECT A,
V$SESSION S WHERE A.SESSION_ID=S.SID
查出被lock 的對象
然後 alter system kill session 'sid,serial#';
14.
查詢用戶所在的表空間:
select username,default_tablespace from dba_users order by username;
15.
添加表字段:
alter table GSJ_WSFWT_KYDJ_HHRTZQK add (HHRZJHM varchar2(20),HHRZJZL varchar2(20) );
修改表字段:
alter table GSJ_WSFWT_DM_ZJ modify (ZJ_DM char(3))。
16
創建臨時表 :
CREATE GLOBAL TEMPORARY table tabxml (str1 clob )
ON COMMIT DELETE ROWS
23.sql計算;to_char((B-C)/decode(C,0,1,C)*100,'FM999990.00') D,
24. 查詢12個月之前的