107. View the Exhibit and examine the structure and data in the INVOICE table.
Which two SQL statements would execute successfully? (Choose two.)
A. SELECT AVG(inv_date ) 类型不一致,不能对date求平均值
FROM invoice;
B. SELECT MAX(inv_date),MIN(cust_id)
FROM invoice;
C. SELECT MAX(AVG(SYSDATE - inv_date)) 缺少group by
FROM invoice;
D. SELECT AVG( inv_date - SYSDATE), AVG(inv_amt)
FROM invoice;
Answer: BD
答案解析:
A,不能对date类型求平均数
B,MAX,MIN可以对数值和date求最大值和最小值
C,组合函数,需要分组,错误
D,AVG对数值求平均数,正确。
首先创建测试表:
sh@TEST0924> create table invoice
2 (inv_no number(3) not null,
3 inv_date date,
4 cust_id varchar2(4),
5 inv_amt number(8,2)
6 );
Table created.
插入数据后查询:
sh@TEST0924> select * from invoice;
INV_NO INV_DATE CUST INV_AMT
---------- --------- ---- ----------
1 01-APR-07 A10 1000
2 01-OCT-07 B1R 2000
3 01-FEB-07 3000
开始测试:
A答案:AGV函数需要一个数值类型或者可以隐式转为数值类型的参数
sh@TEST0924> SELECT AVG(inv_date ) FROM invoice;
SELECT AVG(inv_date ) FROM invoice
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
AGV function takes as an argument any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. The function returns the same data type as the numeric data type of the argument.
B答案:
sh@TEST0924> SELECT MAX(inv_date),MIN(cust_id)
2 FROM invoice;
MAX(INV_D MIN(
--------- ----
01-OCT-07 A10
C答案:缺少group 不要函数
sh@TEST0924> SELECT MAX(AVG(SYSDATE - inv_date)) FROM invoice;
SELECT MAX(AVG(SYSDATE - inv_date)) FROM invoice
*
ERROR at line 1:
ORA-00978: nested group function without GROUP BY
sh@TEST0924> SELECT MAX(AVG(SYSDATE - inv_date)) FROM invoice
2 group by cust_id;
MAX(AVG(SYSDATE-INV_DATE))
--------------------------
2427.62578
可以单独求平均值和最大值。
sh@TEST0924> SELECT AVG(SYSDATE - inv_date)FROM invoice;
AVG(SYSDATE-INV_DATE)
---------------------
2327.29273
sh@TEST0924> SELECT MAX(SYSDATE - inv_date) FROM invoice;
MAX(SYSDATE-INV_DATE)
---------------------
2427.62655
D答案:
sh@TEST0924> SELECT AVG( inv_date - SYSDATE), AVG(inv_amt) FROM invoice;
AVG(INV_DATE-SYSDATE) AVG(INV_AMT)
--------------------- -----------
-2327.2942 2000