日期:2014-05-16 浏览次数:20452 次
WITH TEMP(ID,USER) AS( SELECT ‘01’,’SAM’ FROM DUAL UNION ALL SELECT ‘02’,’MIKE’ FROM DUAL UNION ALL SELECT ‘03’,’TOM’ FROM DUAL UNION ALL SELECT ‘04’,’JANE’ FROM DUAL ) SELECT * FROM TEMP;
CREATE TABLE USER ( NAME VARCHAR(20) NOT NULL,--姓名 DEGREE INTEGER NOT NULL,--学历(1、专科2、本科3、硕士4、博士) STARTWORKDATE date NOT NULL,--入职时间 SALARY1 FLOAT NOT NULL,--基本工资 SALARY2 FLOAT NOT NULL--奖金 );
SELECT NAME,DEGREE,YEAR(STARTWORKDATE) AS WORDDATE, SALARY1+SALARY2 AS SALARY FROM USER WHERE DEGREE IN (3,4);
SELECT DEGREE,YEAR(STARTWORKDATE) AS WORDDATE, AVG(SALARY1+SALARY2) AS AVG_SALARY FROM USER WHERE DEGREE IN (3,4) GROUP BY DEGREE,YEAR(STARTWORKDATE)
WITH TEMP1(NAME,DEGREE,WORDDATE,SALARY) AS ( SELECT NAME,DEGREE,YEAR(STARTWORKDATE) AS WORDDATE, SALARY1+SALARY2 AS SALARY FROM USER WHERE DEGREE IN (3,4) ), TEMP2 (DEGREE,WORDDATE,AVG_SALARY) AS ( SELECT DEGREE,YEAR(STARTWORKDATE) AS WORDDATE, AVG(SALARY1+SALARY2) AS AVG_SALARY FROM USER WHERE DEGREE IN (3,4) GROUP BY DEGREE,YEAR(STARTWORKDATE) ) SELECT NAME FROM TEMP1, TEMP2 WHERE TEMP1.DEGREE=TEMP2.DEGREE AND TEMP1.WORDDATE=TEMP2.WORDDATE AND SALARY<AVG_SALARY;
WITH TEMP1(NAME,DEGREE,WORDDATE,SALARY) AS ( SELECT NAME,DEGREE,YEAR(STARTWORKDATE) AS WORDDATE, SALARY1+SALARY2 AS SALARY FROM USER WHERE DEGREE IN (3,4) ), TEMP2 (DEGREE,WORDDATE,AVG_SALARY) AS ( SELECT DEGREE,WORDDATE, AVG(SALARY) AS AVG_SALARY FROM TEMP1 GROUP BY DEGREE,WORDDATE ) SELECT NAME FROM TEMP1, TEMP2 WHERE TEMP1.DEGREE=TEMP2.DEGREE AND TEMP1.WORDDATE=TEMP2.WORDDATE AND SALARY<AVG_SALARY;
CREATE TABLE BBS ( PARENTID INTEGER NOT NULL, ID INTEGER NOT NULL, NAME VARCHAR(200) NOT NULL ); insert into bbs (PARENTID,ID,NAME) values (0,1,'数据库开发'); insert into bbs (PARENTID,ID,NAME) values (1,11,'DB2'); insert into bbs (PARENTID,ID,NAME) values (11,111,'DB2 文章1'); insert into bbs (PARENTID,ID,NAME) values (111,1111,'DB2 文章1 的评论1'); insert into bbs (PARENTID,ID,NAME) values (111,1112,'DB2 文章1 的评论2'); insert into bbs (PARENTID,ID,NAME) values (11,112,'DB2 文章2'); insert into bbs (PARENTID,ID,NAME) values (1,12,'Oracle'); insert into bbs (PARENTID,ID,NAME) values (0,2,'Java 技术'); COMMIT;
WITH TEMP(PARENTID,ID,NAME) AS ( SELECT PARENTID,ID,NAME FROM BBS WHERE NAME='DB2'---语句1 UNION ALL---语句2 SELECT B.PARENTID,B.ID,B.NAME FROM BBS AS B, TEMP AS T WHERE B.PARENTI D=T.ID---语句3 ) SELECT NAME FROM TEMP;---语句4