select distinct emp.eid,emp.ename,dept.did,dept.dname,cid,cname,sys.code,sys.value,dt_time from 
ORG_CLASS orgclass,ORG_DEPARTMENT dept,SYS_CODE sys,ORG_EMPLOYEE emp,wfes_personnel where 
orgclass.did=dept.did and sys.code=orgclass.clevel and sys.type='CLEVEL' and orgclass.eid=emp.eid and orgclass.eid='1'1上面这条语句可以执行,但我想按照order by排序,可加上"distinct"就不能用order by,为什么啊?怎么解决?
2语句里有个dt_time,是insert的时候取的sysdate,精确到了秒,我想把重复的记录去掉,但dt_time这个字段除外,是不是要用子查询?

解决方案 »

  1.   

    select distinct temp.* from 
    (select emp.eid,emp.ename,dept.did,dept.dname,cid,cname,sys.code,sys.value,dt_time from
    ORG_CLASS orgclass,ORG_DEPARTMENT dept,SYS_CODE sys,ORG_EMPLOYEE emp,wfes_personnel where
    orgclass.did=dept.did and sys.code=orgclass.clevel and sys.type='CLEVEL' and orgclass.eid=emp.eid and orgclass.eid='1'
    这这个子查询里做order by不就可以了
    )
    as temp
      

  2.   

    子查询里用order by 没有意义(除了增加数据库的负担)。我试了在oracle里的情况,distinct和order by 可以同时使用。不知道你要order by 哪个列?如果实在不行,有个变通的方法,就是:把要order by 的列,放到前边去。select distinct emp.eid,emp.ename,dept.did,dept.dname,cid,  ......
    如果要order by cid,就改成:
    select distinct cid,emp.eid,emp.ename,dept.did,dept.dname,  ......
      

  3.   

    我就是在oracle里使用的order by,可是总是报错
      

  4.   

    order by 的列必须在select中
      

  5.   

    在【子查询】里没必要用order byselect distinct col1,col2,col3,... from (sub select)
    由于有distinct,得到的结果,和下边的顺序相同:
    select distinct col1,col2,col3,... from (sub select) order by col1,col2,col3,....
    所以,【子查询】里的order by就没用啦。
      

  6.   

    在【子查询】里没必要用order byselect distinct col1,col2,col3,... from (sub select)
    由于有distinct,得到的结果,和下边的顺序相同:
    select distinct col1,col2,col3,... from (sub select) order by col1,col2,col3,....
    所以,【子查询】里的order by就没用啦。
      

  7.   

    to trumplet(检查)
    我说的那个子查询里面没有distinct的
      

  8.   

    能否给出一些数据的例子、表的结构,和你出错的sql,以便找错误原因
      

  9.   

    刚才仔细看了一下你的sql。你的from子句里的wfes_personnel表,在select子句、where子句里都没有用到哦ORG_CLASS表在你的查询里,也没多大用处哟把5个表的查询,变成3个表的查询,就简单好多了。
      

  10.   

    问题已经解决了,我把dt_time 去掉就可以查询了,不知道为什么,哎
    感谢各位了,结贴