table
fld1   fld2   fld3
a     10001     0
b     10002     0
a     10003     1
d     10004     1
b     10005     0
f     10006     0
d     10007     1
a     10008     1
效果:(fld3=1)
fld1   count
a        2
b        0
d        1
f        0
要求用 group by  和 count

解决方案 »

  1.   

    select 
    distinct(cyks) cyks,
    (select count(cyks) 
    from
    tab
    where fldOther='aa' and cyks=a.cyks) N'count'
    from tab a
    order by cyks不用Group By
      

  2.   

    select sum(case when fldOther='aa' then 1 else 0 end),cyks 
    from tab group by cyks  order by cyks你测试过没有?
      

  3.   

    declare @t table(fld1 varchar(10),fld2 varchar(10),fld3 varchar(10))
    insert @t select 'a',     '10001',     0
    insert @t select 'b',     '10002',     0
    insert @t select 'a',     '10003',     1
    insert @t select 'd',     '10004',     1
    insert @t select 'b',     '10005',     0
    insert @t select 'f',     '10006',     0
    insert @t select 'd',     '10007',     1
    insert @t select 'a',     '10008',     1 select distinct fld1,[count]=(select count(1) from @t where fld1=a.fld1 and fld3=1) from @t a
      

  4.   

    LZ你给的效果是错的,fld3=1 时d明显有两个~
      

  5.   

    select fld1,sum(fld3) from tablename group by fld1
      

  6.   

    create table #t
    (
     fld1 varchar(10) , fld2 varchar(10) , fld3 varchar(10) 
    )
    go
    insert into #t 
    select 'a','10001' , '0'
    union all
    select 'b','10002' , '0'
    union all
    select 'a','10003' , '1'
    union all
    select 'd','10004' , '1'
    union all
    select 'b','10005' , '0'
    union all
    select 'f','10006' , '0'
    union all
    select 'd','10007' , '1'
    union all
    select 'a','10008' , '1'select fld1 , count(*) as count  from #t  group by fld1
      

  7.   

    select fld1,count(fld3) as[count] from [table]  group by fld1 having fld3=1
      

  8.   

    create table #t
    (
     fld1 varchar(10) , fld2 varchar(10) , fld3 varchar(10) 
    )
    go
    insert into #t 
    select 'a','10001' , '0'
    union all
    select 'b','10002' , '0'
    union all
    select 'a','10003' , '1'
    union all
    select 'd','10004' , '1'
    union all
    select 'b','10005' , '0'
    union all
    select 'f','10006' , '0'
    union all
    select 'd','10007' , '1'
    union all
    select 'a','10008' , '1'
    select * from  #t select B.fld1,isnull(A.num,0) as count
    from
    (select  fld1,count(*) as num from #t where fld3='1' group by fld1) A right join 
    (select distinct fld1 from #t) B on A.fld1=B.fld1
      

  9.   

    declare @t table(fld1 varchar(10),fld2 varchar(10),fld3 varchar(10))
    insert @t select 'a',     '10001',     0
    insert @t select 'b',     '10002',     0
    insert @t select 'a',     '10003',     1
    insert @t select 'd',     '10004',     1
    insert @t select 'b',     '10005',     0
    insert @t select 'f',     '10006',     0
    insert @t select 'd',     '10007',     1
    insert @t select 'a',     '10008',     1select A.fld1,isnull(B.count,0) as count from
    (select distinct fld1 from @t) A left join  
    (select fld1,count=(select count(1) from @t where A.fld1=fld1)-1
    from @t A
    where fld3=1
    group by fld1 ) B
    on A.fld1=B.fld1
      

  10.   


    declare @t table(fld1 varchar(10),fld2 varchar(10),fld3 varchar(10))
    insert @t select 'a',     '10001',     0
    insert @t select 'b',     '10002',     0
    insert @t select 'a',     '10003',     1
    insert @t select 'd',     '10004',     1
    insert @t select 'b',     '10005',     0
    insert @t select 'f',     '10006',     0
    insert @t select 'd',     '10007',     1
    insert @t select 'a',     '10008',     1select fld1,sum(case when fld3=1 then 1 else 0 end) as [count]
    from @t group by fld1  order by fld1--结果
    fld1       count       
    ---------- ----------- 
    a          2
    b          0
    d          2
    f          0(所影响的行数为 4 行)
      

  11.   

    晕,用count
    declare @t table(fld1 varchar(10),fld2 varchar(10),fld3 varchar(10))
    insert @t select 'a',     '10001',     0
    insert @t select 'b',     '10002',     0
    insert @t select 'a',     '10003',     1
    insert @t select 'd',     '10004',     1
    insert @t select 'b',     '10005',     0
    insert @t select 'f',     '10006',     0
    insert @t select 'd',     '10007',     1
    insert @t select 'a',     '10008',     1select fld1,count(distinct case when fld3=1 then fld2 else null end)
     as [count]
    from @t group by fld1  order by fld1--结果
    fld1       count       
    ---------- ----------- 
    a          2
    b          0
    d          2
    f          0(所影响的行数为 4 行)
      

  12.   

    loveangela(非留不可)的不行啊,好象看错了题目啊!
    Haiwer(海阔天空) 都可以~ :)
      

  13.   

    select fld1,count(*) from 
    (select fld1,fld3 from [table] where fld3=1)
    group by fld1
    union 
    select fld1,0 from [table] where fld1 not in 
    (select distinct fld1 from [table] where fld3 = 1)