select sum(datediff(second,stime,etime)/60.0) from callinfo 
where userName='xtcrm' and userid ='010  ' 
and datediff(day, '2010-10-23 0:00:00',calldate)>=0 
and datediff(day,'2010-10-23 0:00:00', calldate)<=0 
and pstate = '呼出' 
select distinct stime from callinfo因为表中有重复数据 所以在计算总和时候无法准确计算,请教在第一个SQL语句前提下 如何去除重复项stime列有重复 其他没有重复
目前callinfo表中需要计算所使用的秒数总和 而开始时间有重复项 所以无法正确计算一共使用多少秒 请教我这个语句应该怎么写??

解决方案 »

  1.   

    就是同一个表中的时间想减 但是表中有数据时重复的 也就是说出了ID和结束时间etime不同 其他都相同 无形中多了几条数据 所以计算出来的结果SUM的值就不对 比如满足条件的应该是5条 但是因为有2条重复数据 所以满足条件的变成6条了 那么用SUM来求和 就肯定不对了 
      

  2.   

    id  callno customNO calldate  pstate callphone stime      etime
    1     11      22    2010       呼出   1111     09:01:01   09:02:02
    2     12      22    2010       呼出   1111     09:03:55   09:10:05
    3     13      22    2010       呼出   1111     09:03:55   09:10:22
      

  3.   

    and datediff(day, '2010-10-23 0:00:00',calldate)>=0 
    and datediff(day,'2010-10-23 0:00:00', calldate)<=0  
    的意思跟
    and datediff(day, '2010-10-23 0:00:00',calldate)=0 
    的区别是什么?
      

  4.   

    最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  5.   

    select sum(datediff(second,stime,etime)/60.0) from
    (SELECT distinct stime,etime FROM callinfo ) AS 
     callinfo 
    where userName='xtcrm' and userid ='010 ' 
    and datediff(day, '2010-10-23 0:00:00',calldate)>=0 
    and datediff(day,'2010-10-23 0:00:00', calldate)<=0 
    and pstate = '呼出'  
      

  6.   

    加个group by customNO,calldate,pstate,callphone
    --采用最小时间比较,如果stime重复,min值一样的
    select sum(datediff(second,min(stime),min(etime))/60.0) from callinfo  
    where userName='xtcrm' and userid ='010 '  
    and datediff(day, '2010-10-23 0:00:00',calldate)>=0  
    and datediff(day,'2010-10-23 0:00:00', calldate)<=0  
    and pstate = '呼出' 
     group by customNO,calldate,callphone