简体   繁体   English

如何将值与 SQL max(count) 函数匹配?

[英]how to match a value with SQL max(count) function?

I have a orderLine table looks like this我有一个 orderLine 表看起来像这样

在此处输入图片说明

I would like to know which pizza is the best seller, and the quantity of pizza sold.我想知道哪种披萨最畅销,以及售出的披萨数量。

I've tried query:我试过查询:

select sum(quantity), pizza_name  from order_line group by pizza_name;

it returns它返回

在此处输入图片说明

which is almost what I want, But when I start adding Max function, it could not match the pizza name with the total quantity of pizza sold这几乎是我想要的,但是当我开始添加 Max 函数时,它无法将比萨名称与售出的比萨总数匹配

For example:例如:

select MAX(sum(quantity)), pizza_name  from order_line group by pizza_name;

it returns following error:它返回以下错误:

"not a single-group group function" “不是单组组功能”

I guess I could achieve this by using a sub-query, but I have no idea how to do this.我想我可以通过使用子查询来实现这一点,但我不知道如何做到这一点。

You don't need max for this.你不需要max If you only want one pizza, then you can use order by and fetch first 1 row only (or something similar such as limit or top ):如果您只想要一个披萨,那么您可以使用order byfetch first 1 row only (或类似的内容,例如limittop ):

select sum(quantity), pizza_name
from order_line
group by pizza_name
order by sum(quantity)
fetch first 1 row only;

Or, if you want all such pizzas, use rank() :或者,如果您想要所有这些比萨,请使用rank()

select p.*
from (select sum(quantity) as quantity, pizza_name,
             rank() over (order by sum(quantity) desc) as seqnum
      from order_line
      group by pizza_name
     ) p
where seqnum = 1;

Both of the queries give the same desired result两个查询都给出了相同的期望结果

SELECT PIZZA_NAME,
  SUM(QUANTITY) "Total Quant"
FROM Order_line
GROUP BY PIZZA_NAME
ORDER BY "Total Quant" DESC
FETCH FIRST 1 row only;

SELECT PIZZA_NAME, "Total Quantity" FROM (
SELECT PIZZA_NAME,SUM(QUANTITY) "Total Quantity", RANK() OVER (ORDER BY SUM(QUANTITY) DESC) T FROM Order_line GROUP BY PIZZA_NAME
) query1 where query1.T=1 ;

You group by pizza_name to get sum(quantity) per pizza_name.您按pizza_name 分组以获得每个pizza_name 的总和(数量)。

Then you aggregate again by using MAX on the quantity sum, but you don't specify which of the three pizza names to have in the result.然后您通过对数量总和使用 MAX再次聚合,但您没有指定结果中包含三个比萨名称中的哪一个。 You need an aggregate function on pizza_name as well, which you don't have.您还需要一个关于pizza_name 的聚合函数,而您没有。 Hence the error.因此错误。

If you want to use your query, you must apply the appropriate aggregation function on pizza_name, which is KEEP DENSE_RANK FIRST/LAST.如果您想使用您的查询,您必须对pizza_name 应用适当的聚合函数,即KEEP DENSE_RANK FIRST/LAST。

select 
  max(sum(quantity)), 
  max(pizza_name) keep (dense_rank last order by sum(quantity))
from order_line
group by pizza_name;

But on one hand Gordon's queries are more readable in my opinion.但一方面,我认为戈登的查询更具可读性。 And on the other this double aggregation is Oracle specific and not SQL standard.另一方面,这种双重聚合是特定于 Oracle 的,而不是 SQL 标准。 Unexperienced readers may be confused that the query produces one result row in spite of the GROUP BY clause.没有经验的读者可能会对查询产生一个结果行而感到困惑,尽管有 GROUP BY 子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM