select convert(varchar(10),ca.call_time,20) as countDate,callin_dest_number,count(*) as countCallIn,
(select count(*) from as_call where seat_type=1  
and call_type in(1,3,5)
       and call_seat!=-1
       and callin_dest_number=ca.callin_dest_number and user_number not in('02154265719','02154265471')
       and convert(varchar(10),call_time,20)=convert(varchar(10),ca.call_time,20)) as countCallOn, (select count(*) from as_call as c where seat_type=1 
       and call_type in(1,5)
       and record_starttime is null and record_endtime is not null
       and callin_dest_number=ca.callin_dest_number and user_number not in('02154265719','02154265471')
       and convert(varchar(10),call_time,20)=convert(varchar(10),ca.call_time,20)
       and datediff(ss,record_endtime,
(select min(add_time) from as_phone_state where phone_state=10
       and state_id in(select min(state_id) from bst_phone_state 
where worker_id=c.tsr_id and worker_type=c.seat_type
       and seat_number=c.call_seat and phone_state!=11
       and add_time>=c.record_endtime)
))<=3) as count_zhaijifrom as_call as ca
where call_type in(1,3,5)
       and callin_dest_number not in('2134014648') and user_number not in('02154265719','02154265471')
and ca.call_time>='2008-08-01' and ca.call_time<='2008-08-01 23:59:59'
group by convert(varchar(10),ca.call_time,20),callin_dest_number order by countDate

解决方案 »

  1.   

    SELECT套SELECT。。
    改成存储过程用临时表吧。
      

  2.   


    仔细说一下,刚接手的项目,数据库有400W 条数据,查询要两分钟,感觉这个SQL写的有问题;由于是老项目,SQL都是在JSP里拼的,所以不能用存储过程,希望有人帮我看看这个问题,分数不多,10分求助;
      

  3.   


    sql2000以后
    字查询试用cte...
      

  4.   

    汗一个....用的是SQL Server 2000 没有这个函数
      

  5.   


    是老项目,SQL都是在JSP里拼的,所以不能用存储过程
      

  6.   

    拼SQL语句和拼存储过程是一样的吧?反正都是把页面信息传到拼成的语句里再执行。大不了弄个拼SQL的存储过程
      

  7.   


    --把最后的where 条件换成这个试试where call_type in(1,3,5) 
        and callin_dest_number<>'2134014648' and user_number<>'02154265719' and user_number<>'02154265471' 
    and datediff(day,ca.call_time,'2008-08-01')=0
    group by convert(varchar(10),ca.call_time,20),callin_dest_number order by countDate
      

  8.   

    改了一下,先创建了一个临时表:
    select *
    into temp_as_call as ca from as_call  
    where call_type in(1,3,5) 
          and callin_dest_number not in('2134014648') and user_number not in('02154265719','02154265471') 
    and ca.call_time>='2008-08-01' and ca.call_time <='2008-08-01 23:59:59' 
      

  9.   


    select convert(varchar(10),ca.call_time,20) as countDate,callin_dest_number,count(*) as countCallIn, 
    (select count(*) from temp_as_call where seat_type=1  
    and call_type in(1,3,5) 
          and call_seat!=-1 
          and callin_dest_number=ca.callin_dest_number and user_number not in('02154265719','02154265471') 
          and convert(varchar(10),call_time,20)=convert(varchar(10),ca.call_time,20)) as countCallOn, (select count(*) from temp_as_call as c where seat_type=1 
          and call_type in(1,5) 
          and record_starttime is null and record_endtime is not null 
          and callin_dest_number=ca.callin_dest_number and user_number not in('02154265719','02154265471') 
          and convert(varchar(10),call_time,20)=convert(varchar(10),ca.call_time,20) 
          and datediff(ss,record_endtime, 
    (select min(add_time) from as_phone_state where phone_state=10 
          and state_id in(select min(state_id) from bst_phone_state 
    where worker_id=c.tsr_id and worker_type=c.seat_type 
          and seat_number=c.call_seat and phone_state!=11 
          and add_time>=c.record_endtime) 
    )) <=3) as count_zhaiji from temp_as_call as ca group by convert(varchar(10),ca.call_time,20),callin_dest_number order by countDate然后修改: