表A:ID      TheDate                    ………………
1       2008-1-1 9:00:20           ………………
2       2008-1-2 10:10:00          ………………
3       2008-2-1 11:00:40          ………………
4       2008-3-15 15:30:09         ………………………………………………………………现在想要根据TheDate,统计出每个月的记录数量,请问Sql语句怎么写???关注^^^^^

解决方案 »

  1.   

    select convert(varchar(7),TheDate,120) as Mth,sum(qty) 
    from tb 
    group by convert(varchar(7),TheDate,120)
      

  2.   


    select convert(varchar(7),TheDate,120) as Mth,count(*) as num
    from tb 
    group by convert(varchar(7),TheDate,120)
      

  3.   

    select convert(char(7),thedate,120) as theDate,count(1) as  记录数量
    from A
    group by convert(char(7),thedate,120)
      

  4.   

    select convert(varchar(7),TheDate,120) as 年月,count(1) as 记录数量
    from tab
    group by convert(varchar(7),TheDate,120)
      

  5.   

    Select Count(*) From TableName Where Datediff(Month,TheDate,getdate()) = 1
      

  6.   

    declare @t table(id int,TheDate datetime)
    insert @t select 1,'2008-1-1 9:00:20'
    union all select 2,'2008-1-2 10:10:00'
    union all select 3,'2008-2-1 11:00:40'
    union all select 4,'2008-3-15 15:30:09'select convert(char(7),thedate,120),count(thedate)
    from @t
    group by convert(char(7),thedate,120)/*(所影响的行数为 4 行)                    
    ------- ----------- 
    2008-01 2
    2008-02 1
    2008-03 1(所影响的行数为 3 行)
    */