ORACLE SQL總結一:集合函數和多表查詢

一、集合函數總結
1.1 在select從句中沒有使用集合函數的列,就必須出如今group by從句中。即一個列要麼在select從句中使用集合函數,要麼放在group by從句中。可是注意,出如今group by從句中的列不必定要出如今select 中。
1.2 對列進行條件判斷時,出如今group by 中的列必須使用where從句進行判斷,在select從句中使用集合函數的列必須使用having 從句進行判斷。注意where和having的順序,where從句做用與group以前的條件判斷,having從句做用與group以後的條件判斷。
1.3 集合函數,除COUNT,GROUPING,GROUP,其他的函數在計算時都忽略NULL。 sql

例如,下面這句sql錯誤在於having從句中的order_date沒有使用集合函數express

SELECT o.customer_id, oi.product_id, SUM(oi.unit_price*oi.quantity) "Total" FROM order_items oi JOIN orders o ON oi.order_id=o.order_id GROUP BY ROLLUP (o.customer_id,oi.product_id) HAVING MONTHS_BETWEEN(order_date, SYSDATE) <= 6;


1.4 where從句和having從句能夠在SQL語句中一塊兒使用,做用不一樣。
1.5 Grouping 函數使用小結,頗有趣,已發知識庫。
對oracle官方手冊上的解釋翻譯
Purpose

GROUPING distinguishes superaggregate rows from regular grouped rows. GROUP BY extensions such as ROLLUP and CUBE produce superaggregate rows where the set of all values is represented by null. Using the GROUPING function, you can distinguish a null representing the set of all values in a superaggregate row from a null in a regular row.oracle

The expr in the GROUPING function must match one of the expressions in the GROUP BY clause. The function returns a value of 1 if the value of expr in the row is a null representing the set of all values. Otherwise, it returns zero. The data type of the value returned by the GROUPING function is Oracle NUMBER. Refer to the SELECT group_by_clause for a discussion of these terms.ide

翻譯以下:函數

GROUPING用來從分組後的regular行中區別出superaggregate行,regular行、superaggregate行是由GROUP BY的擴展函數如ROLLUP、CUBE產生的,superaggregate行的特色是全部(列)值爲null,regular行的特色是有一個(列)值爲null,使用GROUPING函數能夠從regular行中區別出superaggregate行。、字體

做爲GROUPING函數中的表達式必須是GROUP BY從句中的一項,若是該表達式值對應的行中全部的列值爲空,則GROUPING函數返回1,不然返回0.GROUPING函數返回的數據類型爲NUMBER型。ui

二、多表查詢
2.1 select * from table1,table2 應該使用的是inner join查詢
2.2 select t1.column1,t1.* from table1 t1 join table2 t2 using(column1)   錯誤
使用using()有幾個特色:
一是table1和table2都有該列,而且列名相同;
二是在using從句中出現的列在select語句中出現時不能使用tablename.column或tablenamealias.column;
三是using從句中不能使用tablename.column或tablenamealias.column。this

2.3 NATURAL JOIN
摘自oralce sql references 中 select語法解釋部分:
....................
The NATURAL keyword indicates that a natural join is being performed. A natural join is based on all columns in the two tables that have the same name. It selects rows from the two tables that have equal values in the relevant columns. If two columns with the same name do not have compatible data types, then an error is raised. When specifying columns that are involved in the natural join, do not qualify the column name with a table name or table alias.lua

On occasion, the table pairings in natural or cross joins may be ambiguous. For example, consider the following join syntax:spa

a NATURAL LEFT JOIN b LEFT JOIN c ON b.c1 = c.c1
This example can be interpreted in either of the following ways:

a NATURAL LEFT JOIN (b LEFT JOIN c ON b.c1 = c.c1)
   (a NATURAL LEFT JOIN b) LEFT JOIN c ON b.c1 = c.c1
To avoid this ambiguity, you can use parentheses to specify the pairings of joined tables. In the absence of such parentheses, the database uses left associativity, pairing the tables from left to right.

翻譯以下

.........
NATURAL關鍵字使得執行natural join,它是基於兩個表中全部相同列名的列,它從兩個表中查詢行,這些表的列(列名相同)有相同的值。
若是兩個列(分別在不一樣的表中)有相同的列名可是數據類型不兼容,使用natural join時會發生錯誤。
當在natural join使用某列聯接時,該列(在select從句)不能使用表名.列名或表名的同義詞.列名這種命名方式。————全部的join都有此項要求。

可是,有時使用natural或cross join進行表之間匹配比較難理解,好比以下的例子:

a NATURAL LEFT JOIN b LEFT JOIN c ON b.c1 = c.c1

這個例子能夠解釋爲下面兩種方式

解釋一:a NATURAL LEFT JOIN (b LEFT JOIN c ON b.c1 = c.c1)
解釋二:(a NATURAL LEFT JOIN b) LEFT JOIN c ON b.c1 = c.c1

爲了不解釋上的模糊不清,你可使用圓括號()來定義匹配的表,在缺乏圓括號時,oracle從左自右解析表達式,好比上面的例子應該解釋二。
2.4 當兩個表使用join從句聯接,而且兩個表有相同的列名,在select 從句中要指明表名,例子以下
select order_id,product_id,unit_price*quantity "total" from order_items oi join orders o on(o.order_id=oi.order_id) where o.order_date>sysdate-7
       *
ERROR at line 1:
ORA-00918: column ambiguously defined

2.5 下面這條語句爲何可執行?and怎麼能放在那裏?
答:是condition語句,將多個條件用and組合,OR等其餘操做符應該也能夠。
SQL> select i.product_id,i.quantity_on_hand,pi.supplier_id from product_information pi join inventories i on(pi.product_id=i.product_id) andquantity_on_hand<5;

2.6 ANY/SOME與ALL的區別,紅色字體部分不一樣
ANY/SOME

Compares a value to each(任何一個) value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Can be followed by any expression or subquery that returns one or more values.
Evaluates to FALSE if the query returns no rows.

ALL
Compares a value to every(每個,全部的) value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Can be followed by any expression or subquery that returns one or more values.
Evaluates to TRUE if the query returns no rows.

2.7 下面這句話是什麼意思?
If the subquery returns 0 rows, then the value returned by the subquery expression is NULL.
若是子查詢返回0行,則子查詢表達式返回的值是NULL

2.8 WITH從句
當查詢中屢次用到某一部分時,能夠用Oracle with語句建立一個公共臨時表。由於子查詢在內存臨時表中,避免了重複解析,因此執行效率會提升很多。臨時表在一次查詢結束自動清除。

通常語法格式:

with  
alias_name1 as    (subquery1), 
alias_name2 as    (subQuery2), 
…… 
alias_nameN as    (subQueryN) 
select col1,col2…… col3  
     from alias_name1,alias_name2……,alias_nameN
Oracle with語句的例子:

SQL> WITH 
Q1 AS (SELECT 3 + 5 S FROM DUAL), 
    Q2 AS (SELECT 3 * 5 M FROM DUAL), 
    Q3 AS (SELECT S, M, S + M, S * M FROM Q1, Q2) 
SELECT * FROM Q3;
輸出結果:

S M S+M S*M 
---------- ---------- ---------- ---------- 
8 15 23 120 

2.9 下面這句話是什麼意思?翻譯正確嗎? A subquery is called a single-row subquery when The inner query returns a single value to the main query 當內部查詢返回單個值給主查詢時,子查詢被稱爲單行查詢。

相關文章
相關標籤/搜索