select 公司名 count(结算类型) from table where 结算类型=1 group by 公司名  a
select 公司名 count(结算类型) from table where 结算类型=2 group by 公司名  b最后
select a.count(),b.count() from a.,b. where a.公司名=b.公司名

解决方案 »

  1.   

    select  公司名 ,(select count(结算类型)from table where 结算类型=1 group by 公司名  ),(select count(结算类型)from table where 结算类型=2 group by 公司名  ),from table
    group by 公司名
      

  2.   

    select  公司名 ,(select count(*)from table a where 结算类型=1 and a.公司名=b.公司名  ),(select count(*)from table c where 结算类型=2 and c.公司名 =b.公司名 ),from table b
    group by 公司名
      

  3.   

    TRANSFORM Count(结算类型) AS 类型
    SELECT 公司名
    FROM table
    GROUP BY 公司名
    PIVOT 结算类型适用于多种结算类型
      

  4.   

    declare @sql varchar(8000)
    set @sql = 'select 公司名'
    select @sql = @sql + ',sum(case 结算类型 when '''+结算类型+''' then 1 end) ['+结算类型+']'
     from (select distinct 结算类型 from a4) as a
    select @sql = @sql+' from a4 group by 公司名'
    exec(@sql)
      

  5.   

    select  公司名 ,(select count(*)from table tb1 where 结算类型=1 and tb1.公司名=tb.公司名  ) as 结算类型1 ,(select count(*)from table tb2 where 结算类型=2 and tb2.公司名 =tb.公司名 ) as 结算类型2,from table tb
    group by 公司名
      

  6.   

    wtadminxjeri(P42.4)
    方法更好!!
      

  7.   

    declare @sql varchar(8000)
    set @sql = 'select 公司名'
    select @sql = @sql + ',sum(case 结算类型 when '''+结算类型+''' then 1 end) ['+'结算方式'+结算类型+']'
     from (select distinct 结算类型 from a4) as a
    select @sql = @sql+' from a4 group by 公司名'
    exec(@sql)