Oracle With 文法 樣本

來源:互聯網
上載者:User

關鍵字: ORACLE

Oracle with語句樣本

WITH a AS (SELECT * FROM bd_member WHERE ROWNUM<10),
b AS (SELECT * FROM tp_trade_card)
select A.MEMBER_NAME,
B.CARD_NO
from A,
B
where A.BD_MEMBER_ID = B.BD_MEMBER_ID

 

Google Search: Oracle +with 視圖就可以得到結果...

 

 

Oracle WITH clause


Oracle Tips by Burleson Consulting

 

 

About Oracle WITH clause

Starting
in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH
clause”, a tool for materializing subqueries to save Oracle from having
to re-compute them multiple times.

The SQL “WITH clause” is very similar to the use of Global temporary
tables (GTT), a technique that is often used to improve query speed for
complex subqueries. Here are some important notes about the Oracle
“WITH clause”:

   • The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
   • Formally, the “WITH clause” is called subquery factoring
   • The SQL “WITH clause” is used when a subquery is executed multiple times
   • Also useful for recursive queries (SQL-99, but not Oracle SQL)

To keep it simple, the following example only references the
aggregations once, where the SQL “WITH clause” is normally used when an
aggregation is referenced multiple times in a query.

We
can also use the SQL-99 “WITH clause” instead of temporary tables. The
Oracle SQL “WITH clause” will compute the aggregation once, give it a
name, and allow us to reference it (maybe multiple times), later in the
query.

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”:

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”:/

 

 

_________________________________________________________________

URL: http://kapokfly.javaeye.com/blog/48406

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.