select a.type,count(*) from
(select 1 type union select 2 type union select 3) a left join [table] on a.type=[table].type group by a.type

解决方案 »

  1.   

    1,建序数表
    select top 8000 identity(int,1,1) as N into numtab from 
    (select top 100 id=1  from sysobjects) as a,
    (select top 100 id=1  from sysobjects) as b,
    (select top 100 id=1  from sysobjects) as c2,
    select a.type,a.count(*) from table right join Numtab b on a.type =b.N
     where B.N in ('1','2','3') group by a.type,b.N
      

  2.   

    create table #tmp(type char(1))
    insert into #tmp(type) values('1')
    insert into #tmp(type) values('2')
    insert into #tmp(type) values('3')select a.type,isnull(count(b.*),0) from #tmp a left join table b 
    on a.type=b.type
      

  3.   

    pengdali(大力 V2.0)
    报告说找不到a.type,为什么?
    有什么简单的办法吗
      

  4.   

    create table #t(type char(1))insert #t select '1'
    insert #t select '1'
    insert #t select '1'
    insert #t select '1'
    insert #t select '2'
    insert #t select '2'select A.type,sum(case when #t.type is null then 0 else 1 end) as [count(*)] from #t right join (select '1' type union select '2' union select '3') as A
     on A.type = #t.type
     where A.type in ('1','2','3') group by A.type type count(*)    
    ---- ----------- 
    1    4
    2    2
    3    0(所影响的行数为 3 行)
      

  5.   

    把蚂蚁gg的改一下
    select b.n,sum( case when a.type is not null then 1 else 0 end)  from Numtab b  left join #tmp a on a.type =b.N
    where B.N in ('1','2','3')
    group by b.n
      

  6.   

    select a.type,count(*) from (select 1 type union select 2 type union select 3) a left join 你的表 b on a.type=b.type group by a.type除了“你的表” 其他都不要改
      

  7.   

    时用存储过程,按type循环,如果记录为零就显示为0;
      

  8.   

    select type,count(type) 
    from table 
    group by tpye
      

  9.   

    left jion 左相关
    light jion 右相关错了不管
      

  10.   

    select type,count(tpye) from table where tpye in ('1','2','3')
    这样就没问题了
      

  11.   

    错了就在这里来。HELP  ME