distinct能否对单个字段过滤?

fool 2004-12-09 10:44:25
记得以前有人发过帖子,大概是几个人打牌,记录分数:
id name score
要列出每个人最高分的sql,用了嵌套实现了。

有个人跟贴写了一条类似
select (distinct on name), id, score from table1 order by score desc
的语句,声称可以先按分数排序,然后寻找不同名字的最高分,过滤掉后面同名的记录,就可以了。
当时没有计较。

谁记得比较清楚那篇帖子?能告诉我应该怎样对单个字段过滤吗?不用嵌套能实现?
...全文
672 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengg21 2004-12-09
  • 打赏
  • 举报
回复
select name,max(score) score from tb group by name order by score desc;
这才是求最大值的正确方法。
楼主需要通过过滤的方式求最大值是不合理的。
liuyi8903 2004-12-09
  • 打赏
  • 举报
回复
多了个where条件.我作测试用的.去掉就行了.
liuyi8903 2004-12-09
  • 打赏
  • 举报
回复
或者
Select * From (
select tb.*,rank() over(partition by name order by score desc) id from tb Where ljjc>0) t
where t.id=1;
ORARichard 2004-12-09
  • 打赏
  • 举报
回复
select name,max(score) score from tb group by name order by score desc;
bzszp 2004-12-09
  • 打赏
  • 举报
回复
select * from (
select tb.*,row_number() over(partition by name order by score desc) id from tb) t
where t.id=1;
chao778899 2004-12-09
  • 打赏
  • 举报
回复
估计要用嵌套吧,帮你顶

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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