不好意思! 上边的select COUNT(*)  应该为 sum(countNumber) ! 特此更正!

解决方案 »

  1.   

    试试这样
    select 日期,
    类型A=sum(case type when A then CountNumber else 0 end),
    类型B=sum(case type when B then CountNumber else 0 end),
    类型C=sum(case type when C then CountNumber else 0 end)
    From tableA
      

  2.   

    select convert(char(10),countDate,120) as countDate,type,sum(countNumber) from tableA group by countDate,type這樣應該沒問題了吧
      

  3.   

    select convert(char(10),countDate,120),
    类型A=sum(case type when A then CountNumber else 0 end),
    类型B=sum(case type when B then CountNumber else 0 end),
    类型C=sum(case type when C then CountNumber else 0 end)
    From tableA
    group by convert(char(10),countDate,120)
      

  4.   


    select * into #temp from (select convert(char(10),countDate,120) as countDate,type,sum(countNumber) from tableA group by countDate,type) as R
    你在來一個行列轉換
    declare @sql varchar(8000)
    set @sql = 'select countDate'
    select @sql = @sql + ',sum(case type when '''+type+''' then c end)+'as '+type
     from (select distinct type from #temp) as T
    select @sql = @sql+' from #temp group by type'
    exec(@sql)這樣寫不知道和樓主得效率誰高呢?望樓主測試後告訴一下
      

  5.   

    第1句你改一下select * into #temp from (select convert(char(10),countDate,120) as countDate,type,sum(countNumber) from tableA group by convert(char(10),countDate,120),type) as R