现在的问题是怎样选出每一大类(NClass)的最佳销售?
现有一产品表如下
TBA
PIdNClasspricesellcount
111145
222124
3125454
45545
511451
655454
.........(省略N多数据)
select NClass ,buycount from tba order by buycount desc
[这个列出的是每一件(PID)的销售排行]
现在的问题是怎样选出每一大类(NClass)的最佳销售?(即每个大类NClass里只选出一个最大的Sellcount值)
[解决办法]
select NClass, max(Sellcount) from TBA group by NClass
[解决办法]
1.select NClass,max(sellcount) sellcount from TBA group by NClass
2.select * from TBA T where not exists(select 1 from TBA where NClass=T.NClass and sellcount> T.sellcount)
[解决办法]
select nclass, max(Sellcount) as Sellcount from tab group by nclass
[解决办法]
----方法1:
select * from 表 as a where not exists(select 1 from 表 where NClass=a.NClassand sellcount> a.sellcount)
----方法2:
select * from 表 as a where sellcount = (select max(sellcount) from 表 where NClass= a.NClass)
order by PID
----方法3:
select a.* from 表 as a inner join (select PID,max(sellcount) as GetQty from 表 group by sellcount) as b
on b.NClass= a.NClass and a.GetQty = b.GetQty order by a.NClass
[解决办法]
select * from TBA a
where sellcount = (select max(sellcount) from TBA where NClass= a.NClass)
[解决办法]
select NClass, max(Sellcount) from TBA group by NClass