with 別名 as (select * from *) select * from 別名;
with t as (select * from emp where depno=10) select * from t where empno=xxx
幾個表的例子:
例子1:
with wd as (select did,arg(salary) 平均工資 from work), em as (select emp.*,w.salary from emp left join work w on emp.eid = w.eid) select * from wd,em where wd.did =em.did and wd.平均工資>em.salary;
例子2:
with TT as (select to_char(ar.archtime,'MM') as month,--ar.archtime, sum(decode(ar.indexname,'9bf85d01-7c16-43c8-893a-8d23de2aaac2', ar.num,null)) as pingtai, sum(decode(ar.indexname,'90bebfc4-dea0-43c2-8967-6fe49bd573cb', ar.num,null)) as diaotao, sum(decode(ar.indexname,'ab1859d0-f1ad-4a5d-af58-06ffda14e658', ar.num,null)) as tigong, sum(decode(ar.indexname,'97863dc9-3480-447a-bdaf-cf73d34d1bfd', ar.num,null)) as tushu from MC_ARCHIVESDATA ar where to_char(ar.archtime,'yyyy') = '{Year}' group by ar.archtime), AA as (select lpad(level,2,0) as omonth from dual connect by level<13) select AA.omonth,TT.pingtai,TT.diaotao,TT.tigong,TT.tushu from TT right join AA on TT.month = AA.omonth order by AA.omonth
oracle獲取1年12個月:
--從具體的某月獲取12個月(帶年月) SELECT TO_CHAR( add_months(to_date('2012', 'yyyy'), ROWNUM-1), 'YYYY-MM') as Monthly FROM DUAL CONNECT BY ROWNUM <= (select months_between(to_date('2013', 'yyyy'), to_date('2012', 'yyyy')) from dual) --從1月起獲取之后的12個月 select '2013-'||lpad(level,2,0) as Monthly from dual connect by level<13
oralce對字段的基本操作:
--修改字段名: alter table <table_name> rename column <column_old> to <column_new>; --修改長度: alter table <table_name> modify <column> <datatype>; --以下操作在 Oracle 10.2.0.1.0 中通過。 /*修改原字段名*/ ALTER TABLE 表名 RENAME COLUMN 字段名 TO 字段名1; /*添加一個和原字段同名的字段*/ ALTER TABLE 表名 ADD 字段名 VARCHAR2(30); /* 將原來的數據更新到新字段中 這是要注意,一定要顯示進行數據類型轉換(不同於MSSQL) */ UPDATE 表名 SET 字段名 = CAST(字段名1 AS VARCHAR2(30)); /*刪除原來的備份字段*/ ALTER TABLE 表名 DROP COLUMN 字段名1;
oracle的遞歸查詢:
表結構
T_XT_JIGOUXINXI:ID CHAR(32) ID JIGOUMING CHAR(20) 機構名稱 FUJIGOU CHAR(32) 所屬機構
數據:
00000000000000000000000000000000 |
吐魯番電業局 |
|
74850da287104c849f9233b83a2e6bff |
輸電運維工區 |
00000000000000000000000000000000 |
19a1b282c84e4d0b93e45de8e217cfc2 |
變電運維工區 |
00000000000000000000000000000000 |
6ac135f8fc0a41fe896b16c3ec41330d |
變電檢修工區 |
00000000000000000000000000000000 |
b12188adf66640e5ac212f51ae393db0 |
鄯善供電局 |
00000000000000000000000000000000 |
909fb66a8b1d46faaf583bdfcbc8e346 |
托克遜供電局 |
00000000000000000000000000000000 |
根據父機構查詢子機構:
select t.id,rpad(' ',level*3-2,' ')||'├'||t.jigouming AS OrgName from t_xt_jigouxinxi t start with trim(t.fujigou)IS Null /*父機構條件*/ connect by prior t.id=t.fujigou;
根據子機構查詢父機構:
select t.id,rpad(' ',level*3-2,' ')||'├'||t.jigouming AS OrgName from t_xt_jigouxinxi t start with trim(t.fujigou) IS NULL connect by prior t.fujigou='19a1b282c84e4d0b93e45de8e217cfc2'; /*需要查詢的子機構的父機構ID*/
順便記一下Sql Server的查詢寫法: 表結構:
CREATE TABLE [dbo].[Organization]( [ID] [int] IDENTITY(1,1) NOT NULL, [OrgName] [nchar](30) NULL, [PrivOrgID] [int] NULL, CONSTRAINT [PK_Organization] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
select (case id when null then '' else space(id) end ) +OrgName as OrgName from Organization