日期:2014-05-19  浏览次数:20489 次

现在的问题是怎样选出每一大类(NClass)的最佳销售?
现有一产品表如下
TBA
PId NClass price sellcount
1 1 1 145
2 2 2 124
3 1 2 5454
4 5 5 45
5 1 1 451
6 5 5 454
.........(省略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