OCP 071 某题解析

View the Exhibit and examine the structure of the ORDER_ITEMS table.

OCP 071 某题解析

 

(Choose the best answer.)

 

You must select the ORDER_ID of the order that has the highest total value among all the orders in the ORDER_ITEMS table.

Which query would produce the desired result?
A. SELECT order_id

FROM order_items

GROUP BY order_id

HAVING SUM(unit_price*quantity) = (SELECT MAX (SUM(unit_price*quantity))

FROM order_items GROUP BY order_id);
B. SELECT order_id

FROM order_items

WHERE(unit_price*quantity) = (SELECT MAX (SUM(unit_price*quantity)

FROM order_items) GROUP BY order_id);

C. SELECT order_id

FROM order_items

WHERE(unit_price*quantity) = MAX(unit_price*quantity)

GROUP BY order_id);

D. SELECT order_id

FROM order_items

WHERE (unit_price*quantity) = (SELECT MAX(unit_price*quantity)

FROM order_items

GROUP BY order_id)

   




Answer: A

 

 Explanation/Reference:

语句分解

这题看着很费劲,,于是创建一个表,并录入随机数据,  注意是 随机数据 

OCP 071 某题解析

0主干语句    是把  the highest total value among all the orders in the ORDER_ITEMS table    对应的order_id 选出来,,,注意关键词   有个“total”。。

因此第一步可以是先找出    the highest total value among all the orders in the ORDER_ITEMS table    的值。。

SELECT MAX (SUM(unit_price*quantity))

FROM order_items 

但注意,,由于有sum   这是个分组函数。。需要有group by  否则会报错。

OCP 071 某题解析

下图是正确的表达方式

OCP 071 某题解析

可以得出最大值是  125189。。

由于125189 这个数值是个聚合函数的值。

 

因此需要把 order_id做了分组  ,候选出三个order_id   ,

OCP 071 某题解析

根据题意,从三个order_id 选出sum(unit_price*quantity)  最大的。。

于是下一步是   
 

 

OCP 071 某题解析

 

得出题目要的答案。。。确认A选项是正确的。。

 

解析出A选项后再看其他选项就发现其他选项 主干 确失 group by 函数了。。均不符合题意。。