分组然后取前几名的问题

buytrip 2009-09-09 09:36:15
数据表的内容大概是一个公司每个地区都有几名销售员,分别选出每个地区的前2名:
area name sales
华东 aa 100
华南 bb 200
华北 kk 2320
华东 kd 3000
华南 tt 251
华南 yt 2548
华北 fsd 655
------------------
我写的SQL语句如下:
select * from table_1 t where name in (sele top 2 name from table_1 where area=t.area order by sales desc)
错误提示为不支持此类查询。
应该如何写?
谢谢
...全文
243 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
buytrip 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wwwwb 的回复:]
这种
Select a.* From table_1 a Left Join table_1 b On a.area=b.area And a.sales <=b.sales Group By a.area,a.sales Having Count(b.area) <=2 Order By a.area,a.sales Desc

不是标准的SQL语法,最好不要用
[/Quote]
嗯,不过如果有时候的确需要的话,就用了,比如说字段太多的情况下,又要选用全部的字段。
wwwwb 2009-09-09
  • 打赏
  • 举报
回复
这种
Select a.* From table_1 a Left Join table_1 b On a.area=b.area And a.sales <=b.sales Group By a.area,a.sales Having Count(b.area) <=2 Order By a.area,a.sales Desc

不是标准的SQL语法,最好不要用
buytrip 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wwwwb 的回复:]
vfp SQL不支持
select * from table_1 t where name in (sele top 2 name from table_1 where area=t.area order by sales desc)

这种格式,用分组效率高一些
[/Quote]
对啊。我也是在网上搜索到的,原来是不能用于VFP SQL,真是可惜,呵呵。也比较好理解,语句也简炼好多。
buytrip 2009-09-09
  • 打赏
  • 举报
回复
谢谢wwwwa,wwwwb,十豆三等高手。再次感谢!
十豆三的问题就解决要取多个字段的问题,如果字段多的话,要一个个的选择就比较麻烦
-----------------------------------------------------------------------
因为高版本的VFP对SQL语句的要求比较严格。
运行不严格的SQL语句前:
sys(3099,70)
*--SQL语句
sys(3099,90)

启用 SQL 数据引擎与 Visual FoxPro 7.0、8.0 或 9.0 兼容。


SYS(3099 [, 70 | 80 | 90])


参数
70

指定 Visual FoxPro 以在 Visual FoxPro 8.0 以前版本中的行为来对待 SQL 命令。
80

指定 Visual FoxPro 以在 Visual FoxPro 8.0 中的行为来对待 SQL 命令。
90

指定 Visual FoxPro 以 Visual FoxPro 9.0 的标准来对待 SQL 命令。(默认)
-------------------------------------------------------------------
原来sys(3099,70)是这个意思,呵呵。
十豆三 2009-09-09
  • 打赏
  • 举报
回复
Sys(3099,70) &&如果是VFP9.0 需要运行此句
Select a.* From table_1 a Left Join table_1 b On a.area=b.area And a.sales<=b.sales Group By a.area,a.sales Having Count(b.area)<=2 Order By a.area,a.sales Desc
wwwwb 2009-09-09
  • 打赏
  • 举报
回复
vfp SQL不支持
select * from table_1 t where name in (sele top 2 name from table_1 where area=t.area order by sales desc)

这种格式,用分组效率高一些
十豆三 2009-09-09
  • 打赏
  • 举报
回复
Select a.area,a.Name,a.sales From table_1 a Left Join table_1 b On a.area=b.area And a.sales<=b.sales Group By a.area,a.Name,a.sales Having Count(b.area)<=2 Order By a.area,a.sales Desc

wwwwb 2009-09-09
  • 打赏
  • 举报
回复
SELECT a.area,a.name,a.sales ,COUNT(*)FROM ff a ;
LEFT JOIN ff b ON a.area=b.area AND a.sales<=b.sales;
GROUP BY a.area,a.name,a.sales HAVING COUNT(b.area)<=2 ORDER BY 1,4
WWWWA 2009-09-09
  • 打赏
  • 举报
回复
SELECT a.area,a.name,a.sales FROM ff a ;
LEFT JOIN ff b ON a.area=b.area AND a.sales<=b.sales;
GROUP BY a.area,a.name,a.sales HAVING COUNT(b.area)<=2

2,748

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧