急:
用的是oracle的数据库
目的是想选出唯一的userid(原有重复值),并且按照filepos排序(原filepos是1,2,3.。。不重复的序号)
问题:以下语句都不能满足要求
1)select distinct userid from bbsfile where rootid=371841(不重复,但是没按顺序排)
2)select distinct userid,filepos from bbsfile where rootid=371841 order by filepos(按顺序排列,但是有重复)
3)select distinct userid from bbsfile where rootid=371841 order by filepos(语法出错)
就是想让出现重复值的一列按照没有重复值得一列排序

解决方案 »

  1.   

    select userid from bbsfile where rootid=371841 group by userid order by min(filepos);
      

  2.   

    按你表达的意思
    2)select distinct userid,filepos from bbsfile where rootid=371841 order by filepos(按顺序排列,但是有重复) 
    是可以达到你要的效果的,贴出数据看看
      

  3.   

    改成
    select userid,filepos from bbsfile t
      where not exists(select 1 from bbsfile
        where userid=t.userid and filepos<t.filepos)
    order by filepos
      

  4.   

    distinct原理应该是先将查询出select  userid from bbsfile where rootid=371841
    的结果集,进行distinct处理,将重复的记录去除
    然后再进行排序 order by filepos
    但是去重后一条记录与distinct前的记录无法一一对应,所以会出错
    如果filepos出现在select后,按照结果集中的字段进行排序,不会有问题。但是不能达到楼主的目的
      

  5.   

    2楼liusong_china的办法简单,有效。直接按照最小的filepos排序就可以了
    5楼wildwave的原理说得很清楚
    谢谢各位了