select errortype as 类型,count(*) as 总数 from errormessage a where a.errortime > '2010-04-07'  group by    a.errortype order by a.errortype 得到的结果是:
  类型    总数
   1      22
   2      13
   3      63
   6      46
   8      12
 select errortype as 类型,count(*) as 已查看数 from errormessage a where a.errortime > '2010-04-07' and  a.lookflag = '1'  group by a.errortype order by a.errortype
得到的结果是:
  类型   已查看数
   1      18
   2      5
   8      8
现在想写一个SQL语句得到以下的结果:
   类型   总数    已查看数
   1      22         18
   2      13          5
   3      63          0
   6      46          0
   8      12          8
下面这个语句不行,请教大家。我SQL基础不行,多谢大家了。
select a.errortype as 类型,count(*) as 总数,b.bb as 已查看数 from errormessage a,(select errortype as 型,
count(*) as bb from errormessage  where errortime > '2010-04-07' and lookflag = '1'  group by errortype  ) b 
where a.errortime > '2010-04-07'  group by a.errortype,b.bb order by a.errortype 

解决方案 »

  1.   

    select errortype as 类型,count(*) as 总数,sum(case when a.lookflag = '1' then 1 else 0 end)as num
     from errormessage a 
    where a.errortime > '2010-04-07' 
    group by a.errortype 
    order by a.errortype  
      

  2.   

    select A.类型 ,A.总数,isnull(B.已查看数,0) as 已查看数
    from (你第一個語句) A
    left join
         (你第二個語句) B
    on A.类型=B.类型
      

  3.   


    select errortype as 类型,count(*) as 总数,sum(case lookflag when '1' then 1 else 0 end ) as 已查看数 from errormessage a where a.errortime > '2010-04-07' group by a.errortype order by a.errortype  
      

  4.   

    两表left join 用关键字连接
      

  5.   

    select A.类型 ,A.总数,isnull(B.已查看数,0) as 已查看数
    from (你第一個語句) A
    left join
         (你第二個語句) B
    on A.类型=B.类型
      

  6.   

    刚才的那个解决了,多谢大家。
    现在就是errortype还有别的值,总数为0目前没显示出来。想得到下面的结果:
    类型 总数 已查看数
    0   0    0
    1   22   18
    2   13   5
    3   63   0
    4   0    0
    5   0    0
    6   46   0
    7   0    0
    8   12   8
    应该怎样修改下SQL语句,解决马上结贴。多谢