SQL SERVER 2000中,一个表有一个字段是datetime型,如果要按照该字段的月份统计,即每年每月分别有多少条记录,这个SQL语句应该怎么写呢?谢谢

解决方案 »

  1.   

    select * from table where month(sj)='1'还要看看你的具体表结构及数据
      

  2.   

    eg.
    表: A  字段:Datetime  條件﹔月份   年份
    ----每月的記錄數
    select 每月的記錄數=count() from 表(nolock)
    where datepart(month,字段)=月份
    ---每年的記錄數
    select 每年的記錄數=count() from 表(nolock)
    where datepart(year,字段)=年份
      

  3.   

    可能是我没说清楚吧,我的意思是,比如
    表A,有一个字段ADDTIME,是Datetime型,现在要写一个SQL语句,返回的记录集形如:
    e.g.
    ADDTIME位于2004年1月的有几条记录,位于2004年2月的有几条记录....
      

  4.   

    select datepart(year,addtime) as 年,datepart(month,addtime) as 月,Sum(1) as 记录数 from 表 group by datepart(year,addtime),datepart(month,addtime)没有环境,不能进行测试,自己试一下
      

  5.   

    to ukyoxh(我来学习),这样写可以通过,但如果我是想把该字段的年月在记录集中合为一个字段显示,我写的是
    select cast(datepart(year,addtime) as varchar(4)) + '年' + cast(datepart(month,addtime) as varchar(2)) + '月' as 月份 ...(后面一样)
    为什么就会提示错误,应该怎么写呢?谢谢
      

  6.   

    Select sum(nNumeric),year(dDate) as year,month(dDate) as month from tTable 
    Group year,month
      

  7.   

    Select count(*),year(dDate) as year,month(dDate) as month from tTable 
    Group year,month
      

  8.   

    select count(*) from table1 group by month(dDate)没测试过的
      

  9.   


    select 年月=convert(char(6),date字段,112),count(*)
    from 表
    group by convert(char(6),date字段,112)
      

  10.   

    楼上的其实对,但我认为112这个类型不太合适,不如用121,更符合国人习惯
    这样获得的月份是形如 “2004-05”的格式,长度为7字节select convert(varchar(7),addtime,121) 月份 ,Sum(1) as 记录数 from 表 
    group by convert(varchar(7),addtime,121)
      

  11.   

    搞定了,呵呵 ,谢谢各位!
    SELECT CAST(YEAR(addDate) AS char(4)) + '年' + CAST(MONTH(addDate) AS char(2)) + '月' AS 月份, COUNT(*) AS 数量 From V_STORAGE GROUP BY CAST(YEAR(addDate) AS char(4)) + '年' + CAST(MONTH(addDate) AS char(2)) + '月'