gpt4 book ai didi

mysql - sql 查询获取按销售额分组的第三高销售额

转载 作者:行者123 更新时间:2023-11-29 06:45:22 24 4
gpt4 key购买 nike

从MySQL表中获取第三高的销售额(按销售额分组)

 select Top 3 * from t1 group by sale_Amnt 

|Id|product_name|product_group |sale_Amnt(INR)
------------------------------------------------
1| p1 | Cosmetic |4485
2| p2 | Cosmetic |8525
3| p3 | Health |12589
4| p4 | Health |8525
5| p5 | Home Appliances|9858
6| p6 | Home Appliances|12589

预期输出

|Id|product_name|product_group    |sale_Amnt(INR)
------------------------------------------------
2| p2 | Cosmetic |8525
4| p4 | Health |8525`

最佳答案

提议的重复项是对问题的误解。该问题似乎正在寻找总体上第三高的值,但考虑到重复项。

您可以在 SQL Server 中使用 offset/fetch 获取第三行:

select t.*
from t
where t.sale_amount = (select t2.sale_amount
from t t2
group by t2.sale_amount
order by t2.sale_amount desc
offset 2 fetch first 1 row only
);

在 MySQL 中,这将是:

select t.*
from t
where t.sale_amount = (select t2.sale_amount
from t t2
group by t2.sale_amount
order by t2.sale_amount desc
limit 1 offset 2
);

关于mysql - sql 查询获取按销售额分组的第三高销售额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49732062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com