检查一列中的不同总和与同一列中另一组的不同总和 table
check sum of distincts in one column with a distinct sum of another set in the same table
我有一个 table 如下图所示
我需要编写一个查询,我必须从中获取总数量大于苹果总数量的水果列表。
所以输出应该是:
mango
我如何用苹果的总和来检查不同水果的总和?
我得到了苹果部分的总和,但我无法完成这一部分。
只需将 GROUP BY
与 HAVING
子句一起使用 :
select fruit_name
from fruit_table
group by fruit_name
having sum(quantity) > (select sum(quantity) from fruit_table where fruit_name = 'apple');
您可以使用 having clause with sum(quantity) conditio
n,这将为您提供芒果作为输出!!
select fruit_name from (select fruit_name,sum(quantity) from fruits_table group by fruit_name
having sum(quantity) > (select quantity from fruits table where
fruit_name = 'apple'));
我有一个 table 如下图所示
我需要编写一个查询,我必须从中获取总数量大于苹果总数量的水果列表。
所以输出应该是:
mango
我如何用苹果的总和来检查不同水果的总和? 我得到了苹果部分的总和,但我无法完成这一部分。
只需将 GROUP BY
与 HAVING
子句一起使用 :
select fruit_name
from fruit_table
group by fruit_name
having sum(quantity) > (select sum(quantity) from fruit_table where fruit_name = 'apple');
您可以使用 having clause with sum(quantity) conditio
n,这将为您提供芒果作为输出!!
select fruit_name from (select fruit_name,sum(quantity) from fruits_table group by fruit_name
having sum(quantity) > (select quantity from fruits table where
fruit_name = 'apple'));