分类汇总并取出每个类中 的前三条记录

tmyd 2004-04-25 07:45:41
表:t_order
字段:c_id,c_name,c_total
要求按照c_total的从高到低选取每人的前三条记录,并且以c_name分组

不知道表诉清楚了没有?
...全文
169 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rewiah 2004-04-25
  • 打赏
  • 举报
回复
不好意思,dotbyte说得对,应该用主键。

select * from t_order a
where c_id in (select top 3 c_id from t_order where c_name=a.c_name order by c_total desc )
order by c_name,c_total desc

人哪,一自以为是就会犯错

真诚向dotbyte道歉,哪位版主有空帮我把上面得回复删除,非常感谢!!


Rewiah 2004-04-25
  • 打赏
  • 举报
回复
dotbyte:试过之后再说话
楼主更应该这样
tmyd 2004-04-25
  • 打赏
  • 举报
回复
c_id是主键
dotbyte 2004-04-25
  • 打赏
  • 举报
回复
楼上:c_total应该是不正确的,要用c_id,因为分相同的应该会有
楼主也不说一下,哪个是主键
Rewiah 2004-04-25
  • 打赏
  • 举报
回复
select * from t_order a
where c_total in (select top 3 c_total from t_order where c_name=a.c_name order by c_total desc )
order by c_name,c_total desc

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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