OracleWith语句语法及示例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
OracleWith语句语法及示例
Oracle With 语句语法及示例
1、一个完整的Oracle With 语句实例:
insert into sms_tmp_stop_circlenum_zsdx –将下面查询的结果插入到临时表中
WITH selectdata --Oracle With开始的查询语句
AS
(SELECT TRUNC(createtime) senddate,agentid,srcnum,
ROUND(SUM(CASE WHEN result='4' THEN 1 ELSE 0 END)/COUNT(*)*100,2) AS ratio
FROM
ZSDX_SMS_OTHERSEND_DETAILS WHERE agentid LIKE 'zsdx%' AND createtime>=TO_DATE('2010-12-30 00:00:00','yyyy-mm-dd hh24:mi:ss')
AND createtime
WITH sum_sales AS
( select /*+ materialize */ sum(quantity) all_sales from stores ),
number_stores AS
( select /*+ materialize */ count(*) nbr_stores from stores ), sales_by_store AS
( select /*+ materialize */ store_name, sum(quantity) store_sales from store natural join sales )
SELECT store_name
FROM store, sum_sales, number_stores, sales_by_store
where store_sales > (all_sales / nbr_stores);
Note the use of the Oracle undocumented "materialize" hint in the "WITH clause". The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary
tables that are created inside the "WITH" clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.
It should be noted that the "WITH clause" does not yet fully-functional within Oracle SQL and it does not yet support the use of "WITH clause" replacement for "CONNECT BY" when performing recursive queries.
To see how the "WITH clause" is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick's great work "Understanding the WITH Clause" showing the use of the SQL-99 "WITH clause" to traverse a recursive bill-of-materials hierarchy
The SQL-99 "WITH clause" is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the "WITH clause" to start our SQL query, defining the aggregations, which can then be named in the main query as if they were "real" tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let's replace the temporary tables with the SQL "WITH" clause":
Link:/doc/0b16980966.html,/t_with_clause.htm Improving Query Performance with the SQL WITH Clause
Oracle9i significantly enhances both the functionality and performance of SQL to address the requirements of business
intelligence queries. The SELECT statement's WITH clause, introduced in Oracle9i, provides powerful new syntax for enhancing query performance. It optimizes query speed by eliminating redundant processing in complex queries.
Consider a lengthy query which has multiple references to a single subquery block. Processing subquery blocks can be costly, so recomputing a block every time it is referenced in the SELECT statement is highly inefficient. The WITH clause enables a SELECT statement to define the subquery block at the start of the query, process the block just once, label the results, and then refer to the results multiple times.
The WITH clause, formally known as the subquery factoring clause, is part of the SQL-99 standard. The clause precedes the SELECT statement of a query and starts with the keyword "WITH." The WITH is followed by the subquery definition and a label for the result set. The query below shows a basic example of the clause:
WITH channel_summary AS
( SELECT channels.channel_desc,
SUM(amount_sold) AS channel_total
FROM sales, channels
WHERE sales.channel_id = channels.channel_id
GROUP BY channels.channel_desc )
SELECT channel_desc, channel_total
FROM channel_summary
WHERE channel_total >
( SELECT SUM(channel_total) * 1/3
FROM channel_summary );
This query uses the WITH clause to calculate the sum of sales for each sales channel and label the results as channel_summary.
Then it checks each channel's sales total to see if any channel's sales are greater than one third of the total sales. By using the new clause, the channel_summary data is calculated just once, avoiding an extra scan through the large sales table.
Although the primary purpose of the WITH clause is performance improvement, it also makes queries easier to read, write and maintain. Rather than duplicating a large block repeatedly through a SELECT statement, the block is localized at the very start of the query. Note that the clause can define multiple subquery blocks at the start of a SELECT statement: when several blocks are defined at the start, the query text is greatly simplified and its speed vastly improved.
The SQL WITH clause in Oracle9i significantly improves performance for complex business intelligence queries. Together with the many other SQL enhancements in Oracle9i, the WITH clause extends Oracle's leadership in business intelligence.。