mysql中具有最大值的行
我有一张像这样的桌子mysql中具有最大值的行,mysql,sql,Mysql,Sql,我有一张像这样的桌子 sales units 100 1 200 3 100 2 200 4 100 5 100 1000 我想选择最大单位的最大销售额 对于上面的示例,输出应该是 100 1000 200 4 我尝试使用max函数,它给出了错误的答案 e、 g 200 1000按销售订购,然后按单位订购,并获取第一条记录: select sales, units from TheTable order by sales desc, u
sales units
100 1
200 3
100 2
200 4
100 5
100 1000
我想选择最大单位的最大销售额
对于上面的示例,输出应该是
100 1000
200 4
我尝试使用max函数,它给出了错误的答案
e、 g 200 1000按
销售
订购,然后按单位
订购,并获取第一条记录:
select sales, units
from TheTable
order by sales desc, units desc
limit 1
结果:
sales units
------ ------
200 4
sales units
------ ------
100 1000
200 4
编辑:
对于您想要的新输出,您需要对sales
值进行分组,并使用max
聚合来获得每组中最高的单位
值:
select sales, max(units)
from TheTable
group by sales
order by sales
结果:
sales units
------ ------
200 4
sales units
------ ------
100 1000
200 4
按
销售
订购,然后按单位
订购,并获取第一条记录:
select sales, units
from TheTable
order by sales desc, units desc
limit 1
结果:
sales units
------ ------
200 4
sales units
------ ------
100 1000
200 4
编辑:
对于您想要的新输出,您需要对sales
值进行分组,并使用max
聚合来获得每组中最高的单位
值:
select sales, max(units)
from TheTable
group by sales
order by sales
结果:
sales units
------ ------
200 4
sales units
------ ------
100 1000
200 4
因为你已经改变了你的问题,答案是:
select sales, max(units) as units
from tab
group by sales
因为你已经改变了你的问题,答案是:
select sales, max(units) as units
from tab
group by sales
我明白了,你的问题有点变了。我已经更正了我的答案。我明白了,你的问题有点变了。我已经更正了我的答案。