select distinct 品名,产地,销量 from table a,(select 品名,max(销量) as 销量 from table group by 品名) b where a.品名=b.品名试一下!!
我知道差不多可以这样实现 select * from table1 a where 销量=(select max(销量) from table1 b where 品名=a.品名)但麻烦的是,我这个table1并不是实在的表,而是查询的结果,要是这样,岂不是要做两遍查询。 即 select * from (select ...) a where 销量=(select max(销量) from (select ...) b where 品名=a.品名)?
select * from (select 品名 ,产地,max(销量) as maxxl from table1 group by 品名,产地 )就ok了
select * from (select 品名 ,产地,max(销量) as maxxl from (select ....... from asdfasfasdf) group by 品名,产地) 不糊涂啊,其实理解是时间磨出来的,放分
改正过的!! select * from aa where c in(select max(c) from aa group by a);
sorry: select a.rkbh,b.rkje,max(a.rksl) as maxx from t1 a,t1 b where a.rkbh=b.rkbh and a.rksl=b.rksl group by a.rkbh,a.rkje
to: mywhsw(bamboo) 你的SQL得到的是按品名,产地分组得到的最大销量,针对楼主给出的数据,你的查询结果是 A a 10 A b 4 A c 6 B a 2 B b 8 B c 15因为没有任何两笔记录的品名,产地是相同的!如果楼主想要得到商品销量最好的产地,又不想查询两遍,那么只有对你的子查询动脑筋了,看看能不能在子查询中返回更有用的信息。
select * from aa where c in(select max(c) from aa group by a); 正确
select * from table1 a where 销量=(select max(销量) from table1 b where 品名=a.品名)但麻烦的是,我这个table1并不是实在的表,而是查询的结果,要是这样,岂不是要做两遍查询。
即
select * from (select ...) a where 销量=(select max(销量) from (select ...) b where 品名=a.品名)?
)就ok了
1 R0001 9 54
2 R0001 2 33
3 R0002 21 34
4 R0002 2 45
5 R0003 4 66
6 R0003 5 66
7 R0001 2 98select rkbh,rkje,max(rksl) as maxx from t1 group by rkbh,rkje
执行后的结果如下:R0001 2 98
R0002 2 45
R0003 4 66
R0003 5 66
R0001 9 54
R0002 21 34没有完成统计。
不糊涂啊,其实理解是时间磨出来的,放分
select * from aa where c in(select max(c) from aa group by a);
select a.rkbh,b.rkje,max(a.rksl) as maxx from t1 a,t1 b where a.rkbh=b.rkbh and a.rksl=b.rksl group by a.rkbh,a.rkje
你的SQL得到的是按品名,产地分组得到的最大销量,针对楼主给出的数据,你的查询结果是
A a 10
A b 4
A c 6
B a 2
B b 8
B c 15因为没有任何两笔记录的品名,产地是相同的!如果楼主想要得到商品销量最好的产地,又不想查询两遍,那么只有对你的子查询动脑筋了,看看能不能在子查询中返回更有用的信息。
正确
这样肯定不对
from t1 a,(select 品名,max(销量) from t1 group by 品名) b
where a.品名=b.品名
and a.销量 =b.销量