sql1-ch4 群組函數與聚總資料. 題號 80 題: 33 、 39 140 題: 10 、 59 、 95 、...

20
SQL1-ch4 群群群群群群群群群

Upload: damon-melton

Post on 08-Jan-2018

240 views

Category:

Documents


0 download

DESCRIPTION

群組函數  群組函數 (group function)  聚總函數 (aggregate function)  每一組資料列只會有一個結果

TRANSCRIPT

Page 1: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

SQL1-ch4群組函數與聚總資料

Page 2: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

題號 80題: 33、 39 140題: 10、 59、 95、 110

Page 3: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

群組函數 群組函數 (group function) 聚總函數 (aggregate function)

每一組資料列只會有一個結果

Page 4: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

練習 找出員工資料表中,最資深、資淺員工的聘用日期。

Page 5: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

群組函數 群組函數與空值

Page 6: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

6

Q39/80(可略 )View the Exhibit and examine the description

of the PRODUCT_INFORMATION table.Which SQL statement would retrieve from the

table the number of products having LIST_PRICE as NULL?

Page 7: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

7

A. SELECT COUNT(list_price) FROM product_information WHERE list_price IS NULL;

B. SELECT COUNT(list_price) FROM product_information WHERE list_price = NULL;

C. SELECT COUNT(NVL(list_price, 0)) FROM product_information WHERE list_price IS NULL;

D. SELECT COUNT(DISTINCT list_price) FROM product_information WHERE list_price IS NULL;

the number of products having LIST_PRICE as NULL?

Page 8: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

資料群組 建立資料群組

Page 9: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

9

Q33/80Evaluate the following SQL statements:

Statement 1:SELECT MAX(unit_price*quantity) “Maximum Order”FROM order_items;

Statement 2:SELECT MAX(unit_price*quantity) “Maximum Order”FROM order_itemsGROUP BY order_id;

Which statements are true regarding the output of these SQL statements? (Choose all that apply.)

Page 10: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

10

A. Statement 1 would return only one row of output.

B. Both the statements would give the same output.

C. Statement 2 would return multiple rows of output.

D. Statement 1 would not return any row because the GROUP BY clause is missing.

E. Both statements would ignore NULL values for the UNIT_PRICE and QUANTITY columns.

SELECT MAX(unit_price*quantity) “Maximum Order”FROM order_items;

SELECT MAX(unit_price*quantity) “Maximum Order”FROM order_itemsGROUP BY order_id;

Page 11: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

資料群組 Group by

必須放在 where子句之後 須包含欄位名稱或運算式 不可用欄位別名 可搭配單列函數使用

Select子句中沒有使用聚總函數的欄位,須使用 Group by分群。

Page 12: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

資料群組 依多個欄位分組

Page 13: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

練習 找出每個部門中,最資深、資淺員工的聘用日期。

Page 14: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

Q110/140Which two statements are true regarding the GROUP BY clause

in a SQL statement? (Choose two.)

A. You can use column alias in the GROUP BY clause. B. Using the WHERE clause after the GROUP BY clause excludes the

rows after creating groups. C. The GROUP BY clause is mandatory(強制的 ) if you are using an

aggregate function in the SELECT clause. D. Using the WHERE clause before the GROUP BY clause excludes

the rows before creating groups. E. If the SELECT clause has an aggregate function, then those

individual columns without an aggregate function in the SELECT clause should be included in the GROUP BY clause. (P4-17)

Page 15: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

群組篩選 不能在 where中使用群組函數

Page 16: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

群組篩選 群組條件的篩選,須使用 Having子句

Page 17: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

Q95/140Which three statements are true regarding group

functions(群組函數 )? (Choose three.)

A. They can be used on columns or expressions. B. They can be passed as an argument to another group

function.C. They can be used only with a SQL statement that has the

GROUP BY clause. D. They can be used on only one column in the SELECT clause

of a SQL statement. E. They can be used along with the single-row function in the

SELECT clause of a SQL statement. (P4-10)

Page 18: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

Q10/140Which three statements are true regarding the WHERE and

HAVING clauses in a SQL statement? (Choose three.)

A. The HAVING clause conditions can have aggregate functions. B. The HAVING clause conditions can use aliases for the columns. C. WHERE and HAVING clauses cannot be used together in a SQL

statement. D. The WHERE clause is used to exclude rows before the

grouping of data. E. The HAVING clause is used to exclude one or more aggregated

results after grouping data.

Page 19: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

Q59/140(可略 )View the Exhibit and examine the description of the

PRODUCT_INFORMATION table. Which SQL statement would retrieve from the table the number of products having LIST_PRICE as NULL?

A. SELECT COUNT(list_price) FROM product_information WHERE list_price IS NULL;

B. SELECT COUNT(list_price) FROM product_information WHERE list_price = NULL;

Page 20: SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

C. SELECT COUNT(NVL(list_price, 0)) FROM product_information WHERE list_price IS NULL;

D. SELECT COUNT(DISTINCT list_price) FROM product_information WHERE list_price IS NULL;