表:company id  companyanme   class   date
1     aaaa          A   10/12/2007
2     bbbb          B   10/12/2007
3     sssbbbb       B   10/12/2007
4     aaaa          A   10/13/2007
5     bbbb          B   10/13/2007
6    sssbbbb        B   10/13/2007要求日期          A类   B类     C类    D类       合计 
10/12/2007     2   3    10   20     35  
10/13/2007     2   3    10   20     35  
10/14/2007     6   3    10   20     36  不代条件的可以,但是需要加上class的怎么做呢?
Select Count(id),[date]= Convert(Varchar(8),[date],112) 
from company group by Convert(varchar(8),[date],112)

解决方案 »

  1.   

    用sum()如果为对A类如果为A就+1,否则+0Select [date]= Convert(Varchar(8),[date],112) ,
     sum(case when class='A' then 1 else 0 end) A类,
      sum(case when class='B' then 1 else 0 end) B类,
      sum(case when class='C' then 1 else 0 end) C类,
      sum(case when class='D' then 1 else 0 end) D类,
      Count(id) 合计
    from company group by Convert(varchar(8),[date],112)
      

  2.   

    谢谢解决了我上面的问题,还有一个排序的问题
    Select [date]= Convert(Varchar(8),[date],112) , 
     sum(case when class= 'A ' then 1 else 0 end) A类, 
      sum(case when class= 'B ' then 1 else 0 end) B类, 
      sum(case when class= 'C ' then 1 else 0 end) C类, 
      sum(case when class= 'D ' then 1 else 0 end) D类, 
      Count(id) 合计 
    from company group by Convert(varchar(8),[date],112) 
    这条SQL 语句能用按照[date] 排序吗?如何做呢?
      

  3.   

    后面加一个order by Convert(Varchar(8),[date],112)就可以了吧