sql 怎样得到数据库中满足某一条件的记录为0的数据

解决方案 »

  1.   

    lsh djbh,zwbh,shbz
    1    1    1    0
    1    1    3    0
    2    1    1    0
    3    1    1    1     --------djdtshbselect lsh,djbh,count(*)as counts from 
    djdtshb where shbz=0 group by lsh,djbh这样得到的都是
    lsh,djbh,counts
    1    1     2
    2    1     1
    我想得到
    lsh,djbh,counts
    1    1     2
    2    1     1
    3    1     0
      

  2.   

    你直接:
    select lsh,djbh,count(*)as counts from 
    djdtshb group by lsh,djbh不要条件就行了啊
      

  3.   

    create table tab(lsh int, djbh int,zwbh int,shbz int)
    insert tab
    select 1   , 1  ,  1    ,0
    union select 1 ,   1  , 3  ,  0
    union select 2 ,   1  ,  1 ,   0
    union select 3  ,  1,    1  ,  1     --------djdtshbselect lsh,djbh,sum(case when shbz=0 then 1 else 0 end)
    from tab 
    group by lsh,djbhdrop table tab
      

  4.   

    create table tab(lsh int, djbh int,zwbh int,shbz int)
    insert tab
    select 1   , 1  ,  1    ,0
    union select 1 ,   1  , 3  ,  0
    union select 2 ,   1  ,  1 ,   0
    union select 3  ,  1,    1  ,  1     --------djdtshbselect lsh,djbh,counts=sum(case when shbz=0 then 1 else 0 end)
    from tab 
    group by lsh,djbh
    drop table tab/*    结果    lsh         djbh         counts           
    ----------- ----------- ----------- 
    1           1           2
    2           1           1
    3           1           0(3 row(s) affected)
    */
      

  5.   

    ---把你的语句换成下面的就可以了 select lsh,djbh,counts=sum(case when shbz=0 then 1 else 0 end)
    from tab 
    group by lsh,djbh