select 待发送时间,待发送数量=count(待发送时间),
已发送数量=isnull((select count(已发送时间) from 短信已发送表 where 已发送时间=a.待发送时间 group by 已发送时间),0),已接收数量=isnull((select count(收到信息时间) from 短信收到表 where 收到信息时间=a.待发送时间 group by 收到信息时间),0)  from 短信待发送表 a group by 待发送时间

解决方案 »

  1.   

    --试试我写的这个吧!select 时间=(isnull(isnull(a.时间,b.时间),c.时间)),待发送数量=isnull(a.待发送数量,0),已发送数量=isnull(b.已发送数量,0),已接收数量=isnull(c.已接收数量,0)
    from
    (select 时间=convert(char(8),待发送时间,120),待发送数量=count(*) from 短信待发送表 group by convert(char(8),待发送时间,120)) a full outer join(select 时间=convert(char(8),已发送时间,120),已发送数量=count(*) from 短信待发送表 group by convert(char(8),已发送时间,120)) b on a.时间=b.时间
                                          full outer join
    (select 时间=convert(char(8),收到信息时间,120),已接收数量=count(*) from 短信待发送表 group by convert(char(8),收到信息时间,120)) c on b.时间=c.时间
      

  2.   

    to Softlee81307(孔腎) 
    谢谢楼主的热心帮助。
    刚才试了 感觉结果不对,
    数据库中待发送表有2条记录 已发送表有34记录 接收表有8条记录
    但执行后的结果只有2条记录(即两个日 和待发送表的的日一致)
      

  3.   

    --对上边的语句加以改正.测试成功!select 时间=(isnull(isnull(a.时间,b.时间),c.时间)),待发送数量=isnull(a.待发送数量,0),已发送数量=isnull(b.已发送数量,0),已接收数量=isnull(c.已接收数量,0)
    from 
    (select 时间=convert(char(10),待发送时间,120),待发送数量=count(*) from 短信待发送表 group by convert(char(10),待发送时间,120)) a full outer join(select 时间=convert(char(10),已发送时间,120),已发送数量=count(*) from 短信已发送表 group by convert(char(10),已发送时间,120)) b on a.时间=b.时间
                                          full outer join
    (select 时间=convert(char(10),收到信息时间,120),已接收数量=count(*) from 短信收到表 group by convert(char(10),收到信息时间,120)) c on b.时间=c.时间 or a.时间=c.时间
    --测试结果
    /*
    时间         待发送数量       已发送数量       已接收数量       
    ---------- ----------- ----------- ----------- 
    2005-01-01 2           2           3
    2005-02-05 1           0           1
    2005-01-02 1           1           0
    2005-02-06 0           1           0(所影响的行数为 4 行)
    */