WEB开发网
开发学院数据库DB2 DB2 9 应用开发(733 考试)认证指南,第 2 部分 阅读

DB2 9 应用开发(733 考试)认证指南,第 2 部分

 2009-01-13 16:39:48 来源:WEB开发网   
核心提示: WITH <COMMON NAME1> AS ( <SELECT EXPRESSION>), <COMMON NAME2>AS (<SELECT EXPRESSION), & SELECT <COLUMN> FROM <TABLE

WITH <COMMON NAME1> AS ( <SELECT EXPRESSION>), <COMMON NAME2>
AS (<SELECT EXPRESSION), & SELECT <COLUMN> FROM <TABLE_NAME> <WHERE_CLAUSE>

<table_name> 是数据库中的一个表,也可以是由一个包括 WITH 子句的 SQL 语句定义的 <Common name>。 下面是一个例子:

WITH PROD_QUANTITY AS
(SELECT PRODUCT_ID, SUM (QUANTITY) AS QUANTITY
  FROM CUSTOMER_ORDER_ITEM
  GROUP BY PRODUCT_ID),
 TOTALS AS
(SELECT -1 AS PRODUCT_ID, SUM(QUANTITY) AS TOTAL)
SELECT PRODUCT_ID, QUANTITY 
FROM PROD_QUANTITY
UNION
SELECT PRODUCT_ID, TOTALS
FROM TOTALS
ORDER BY 1 DESC

在上述例子中,prod_quantity 被定义为一个公共表表达式。它与一个名为 totals 的公共表表达式一起使用。最终的 SELECT 语句将从两个公共表表达式中进行选择。

下面看另一个例子:

WITH
 PAYLEVEL AS                        
  (SELECT EMPNO, EDLEVEL, YEAR(HIREDATE) AS HIREYEAR,     
    SALARY+BONUS+COMM AS TOTAL_PAY                  
    FROM EMPLOYEE                            
    WHERE EDLEVEL > 16),                           
 PAYBYED (EDUC_LEVEL, YEAR_OF_HIRE, AVG_TOTAL_PAY) AS    
  (SELECT EDLEVEL, HIREYEAR, AVG(TOTAL_PAY)         
     FROM PAYLEVEL                      
     GROUP BY EDLEVEL, HIREYEAR)
 SELECT EMPNO, EDLEVEL, YEAR_OF_HIRE, TOTAL_PAY, DECIMAL(AVG_TOTAL_PAY,7,2)
  FROM PAYLEVEL, PAYBYED                      
  WHERE EDLEVEL = EDUC_LEVEL                    
    AND HIREYEAR= YEAR_OF_HIRE               
    AND TOTAL_PAY < AVG_TOTAL_PAY            

上一页  2 3 4 5 6 7 8 9 10  下一页

Tags:DB 应用开发 考试

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接