我想用distinct 但不想它排序

chensh1024 2004-11-25 06:17:51
例如表中有数据(是number型):
888
888
888
333
333
222
222
222
111
我想得到
888
333
222
111
...全文
289 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
chensh1024 2004-11-26
  • 打赏
  • 举报
回复
算了,还是用程序控制吧.
chensh1024 2004-11-26
  • 打赏
  • 举报
回复
我试了你的方法,在我做的临时表中确实能成功.
但在我程序中那个查询里面确实不行,不知是不是子查询出来的结果跟真正的表有一些本质的区别
kulama2004 2004-11-26
  • 打赏
  • 举报
回复
select min(rowid) from test t2 group by id
这个subquery得出来的结果是按照id排序的,但是不影响最后的结果,因为外面用的是in
kulama2004 2004-11-26
  • 打赏
  • 举报
回复
A rowid is unique for a row within a table, but not within a database. A rowid consists of three parts: file id, block number and slot in this block. As a slot can be occupied at most by one row, a rowid uniquely identifies a row in a table.
kulama2004 2004-11-26
  • 打赏
  • 举报
回复
rowid代表了记录在表中的位置,不受到group by影响的
chensh1024 2004-11-26
  • 打赏
  • 举报
回复
不行啊!在里面用group by以后,它是先排序在得出min(rowid)
kulama2004 2004-11-26
  • 打赏
  • 举报
回复
刚才用explain plan看了下,第一次用的方法做了两次全表扫描,是很慢的
后面的方法因为是full scan by rowid,所以速度有质的飞跃
kulama2004 2004-11-26
  • 打赏
  • 举报
回复
换成这个
select * from test t1 where rowid in (select min(rowid) from test t2 group by id);
chensh1024 2004-11-26
  • 打赏
  • 举报
回复
kulama2004(kulama) 兄弟的这种办法能解决,但由于我那个有重复的记录集是一个非常复杂的子查询出来的,而且量有上万条记录.用这个方法后慢的不能接受,不知有没有更好的方法
bluelamb 2004-11-26
  • 打赏
  • 举报
回复
select num from tab group by num
chensh1024 2004-11-26
  • 打赏
  • 举报
回复
kulama2004(kulama) 这个兄弟的方法可能有用,我试试先.先谢了
chensh1024 2004-11-26
  • 打赏
  • 举报
回复
不好意思,我没有说清楚.
231
123
888
888
888
333
333
222
222
222
111(这个集合是我通过一系列操作select出来的,包括了order by),然后我想显示出唯一的,group by应该也是排序的.我想得到的结果是子查询的顺序,如下(不想它排序,但又想它唯一):
231
123
888
333
222
111
armyyd 2004-11-25
  • 打赏
  • 举报
回复
如果数据是

888
222
222
222
888
888
333
333
111

那么应该得到的结果是
888
222
333
111
还是
222
888
333
111

如果是前者,用5,6楼的方法可以
如果是后者,得看你的判断条件了。
WorldMobile 2004-11-25
  • 打赏
  • 举报
回复
select column
from table
group by column;

即可
lialin 2004-11-25
  • 打赏
  • 举报
回复
select * from (select a.* ,rank() over (partition by c1/*your columnname*/ order by c2/*primary key*/) rk from testa a) where rk=1;

如果你有一个primary _id列的话可以按这个方法试一下应该可以实现的!
shawnzhao 2004-11-25
  • 打赏
  • 举报
回复
select distinct xx,... from 表名 order by xx desc
kulama2004 2004-11-25
  • 打赏
  • 举报
回复
18:48:09 SQL> select * from test;

ID NAME
----- ----------
2 drop
1 cat
1 hat
3 gg
2 have

Elapsed: 00:00:00.00
18:51:30 SQL> select * from test t1 where exists (select id,min(rowid) from test t2 group by id havi
ng min(rowid) = t1.rowid);

ID NAME
----- ----------
2 drop
1 cat
3 gg

Elapsed: 00:00:00.00
kulama2004 2004-11-25
  • 打赏
  • 举报
回复
select col_name from table_name t1 where exists ( select col_name,min(rowid) from table_name t2 group by col_name having min(rowid) = t1.rowid )
kulama2004 2004-11-25
  • 打赏
  • 举报
回复
group by 是默认按照第一列排序的...
lialin 2004-11-25
  • 打赏
  • 举报
回复
group by
可以解决你的问题了!
加载更多回复(2)

17,078

社区成员

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

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