SELECT merchant_no,
       COUNT(CASE WHEN trans_type=1 THEN 1 END) [trans_type=1的个数],
       COUNT(CASE WHEN trans_type=2 THEN 1 END) [ trans_type=2的个数]
FROM tb 
GROUP BY merchant_no

解决方案 »

  1.   

    select merchant_no,
    sum(case  trans_type when 1 then 1 else 0 end) 'trans_type=1的个数',
    sum(case  trans_type when 2 then 1 else 0 end) 'trans_type=2的个数'
    from line 
    group by merchant_no
      

  2.   

    --Code
    select [merchant_no],
    count1=sum(case [trans_type] when 1 then 1 else 0 end),
    count1=sum(case [trans_type] when 2 then 1 else 0 end)
    from @line a
    group by [merchant_no]--Result
    /*
    merchant_no count1      count1
    ----------- ----------- -----------
    111         1           2
    222         0           1
    */
      

  3.   


    select
      a.merchant_no,
      trans_type_1 =  sum(case when trans_type = 1 then 1 else 0 end),
      trans_type_2 =  sum(case when trans_type = 2 then 1 else 0 end)
    from line a
    group by a.merchant_no
      

  4.   

    create table line(merchant_no int, trans_type int)
    insert into line values(111 ,         1 )
    insert into line values(111 ,         2 )
    insert into line values(111 ,         2 )
    insert into line values(222 ,         2 )
    select merchant_no,
    sum(case  trans_type when 1 then 1 else 0 end) 'trans_type=1的个数',
    sum(case  trans_type when 2 then 1 else 0 end) 'trans_type=2的个数'
    from line 
    group by merchant_no--drop table line/*
    merchant_no trans_type=1的个数 trans_type=2的个数 
    ----------- --------------- --------------- 
    111         1               2
    222         0               1(所影响的行数为 2 行)*/
      

  5.   

    select merchant_no
    ,sum(case when trans_type=1 then 1 else 0 end) [trans_type=1的个数]
    ,sum(case when trans_type=2 then 1 else 0 end) [trans_type=2的个数]
    from line group by merchant_no
      

  6.   

    select merchant_no,sum(case when trans=1 then 1 else 0 end ),sum(case when trans=2 then 1 else 0 end ) from line group by merchant_no
      

  7.   

    select merchant_no,sum(case when trans=1 then 1 else 0 end ),sum(case when trans=2 then 1 else 0 end ) from line group by merchant_no
      

  8.   

    create table line(merchant_no int, trans_type int)
    insert into line values(111 ,         1 )
    insert into line values(111 ,         2 )
    insert into line values(111 ,         2 )
    insert into line values(222 ,         2 )
    select merchant_no,
    sum(case  trans_type when 1 then 1 else 0 end) 'trans_type=1的个数',
    sum(case  trans_type when 2 then 1 else 0 end) 'trans_type=2的个数'
    from line 
    group by merchant_no--drop table line/*
    merchant_no trans_type=1的个数 trans_type=2的个数 
    ----------- --------------- --------------- 
    111         1               2
    222         0               1(所影响的行数为 2 行)*/
      

  9.   

    大家的写法都没有错,但是运行到sybase上确报错,是不是这个 不能用到sybase上呢?我用在虚拟机上的ASE-12的sybase上能用,可是在其他的sybase上没办法用
      

  10.   

    SYBASE11版本的就报错了,各位高手,,怎么办呢?还有其他的办法吗?只能用一条sql语句要么就得建立一个临时表,这样的临时表又该怎么建?
      

  11.   

    开另外一个帖子,终于有答案了,写这里供查询。谢谢所有帮助我的人。sybae11里实现如下: 
    select merchant_no, 
    sum( abs(abs(sign(isnull(trans_type,0)-1))-1) ) count1, 
    sum( abs(abs(sign(isnull(trans_type,0)-2))-1) ) count2 
    from line group by merchant_no