语句如下:
select a.OperCode,a.OperName,sum(a.SPZS) as spzs,sum(a.yxzs) as yxzs,sum(b.SJZS) as sjzs,sum(b.SJZS) / sum(a.yxzs) as dlc
from z_KLHZ a left join z_dayfare b on a.OperCode=b.OperCode and a.departdate=b.DepartDate 
where a.departdate>='2008-3-1' and a.DepartDate<='2008-4-14'
group by a.OperCode,a.OperName
order by sum(b.SJZS) / sum(a.yxzs) desc
结果:
`OperCode`, `OperName`, `spzs`, `yxzs`, `sjzs`, `dlc`
'2014','¶­¿¡    ','10128','7338','1643','0.2239'
'2507','ÍõСÑà  ','18928','12966','3299','0.2544'
'2513','ÕÅÅåºì  ','18668','13230','3721','0.2813'
'2003','ÂéÀö¾ê  ','19933','14721','3735','0.2537'
'2515','ÑîÀöÇÛ  ','17647','13332','2232','0.1674'
其它列都能正常排序,最后一列,为什么不按照指定的sum(b.SJZS) / sum(a.yxzs) desc排序?

解决方案 »

  1.   

    对于sum(b.SJZS) / sum(a.yxzs),
    我加上Convert(sum(b.SJZS),decimal(10,4))/convert(sum(a.yxzs),decimal(10,4))
    也不解决问题。
      

  2.   

    sum(a.yxzs) 是不是有空的或为0[align=center]====  ====
    [/align]
      

  3.   

    试试
    order by sum(b.SJZS) / IFNULL(sum(a.yxzs),0.1) desc [align=center]====  ====
    [/align]
      

  4.   

    a.yxzs 有空或0
    sum(a.yxzs) 无空或0
    sum(b.sjzs) 有0
      

  5.   

    做除法后尽量用函数来排序,比如
    truncate(..,0);
    ...
      

  6.   

    感谢朋友们的回复,按楼上所述
    order by truncate(ifnull(sum(b.SJZS),0)/sum(a.YXZS),4)
    仍不能成功。
      

  7.   


    有试过吗?[align=center]====  ====
    [/align]
      

  8.   

    都试过了。MSSQL 中没遇到过此类问题。
    遇到除法之类的也就是加一个Convert(money,sum(b.sjzs))/Convert(money,sum(a.yxzs))
    没出现过排序出错。郁闷!
      

  9.   

    感谢同志们的回复,yueliangdao0608的"内敛视图来排序"给了我思路,
    问题解决,代码如下:select a.opercode,a.opername,a.yxzs,a.sjzs,a.sjzs/a.yxzs as dcl
    from (select a.OperCode,a.OperName,sum(a.yxzs) as yxzs,sum(b.sjzs) as sjzs
    From z_klhz a left join z_dayfare b on a.departdate=b.departdate and a.OperCode=b.Opercode
    where a.Departdate>='2008-1-1' and a.Departdate<='2008-1-31'
    group by a.OperCode,a.OperName) a
    order by a.sjzs/a.yxzs desc