我有个表,里面有一列是datetime类型,存储的是该行记录添加的时间,我想将表内数据按月分组,并求出列名为 数量 的数据和,也就是统计每月的 数量 和,如何实现?

解决方案 »

  1.   

    select convert(varchar(10),date,120) as date,sum(col) as 合计
    from tb
    group by convert(varchar(10),date,120)
      

  2.   

    select month(时间列) as 月份 ,sum(数量) as 数量
    from tb
    group by month(时间列)
      

  3.   

    select convert(varchar(5),date,23) as date,sum(col) as 合计
    from tb
    where date between '起始日期' and '截止日期'
    group by  convert(varchar(5),date,23)
      

  4.   

    select year(里面有一列)*100+month(里面有一列) ,count(*)
    from 我有个表
    grouup by  year(里面有一列)*100+month(里面有一列) 
      

  5.   

    select sum(数量)
    from 表
    group by convert(varchar(7),日期,121)
      

  6.   


    select
        [月份] = convert(char(6),datetime_col,112)
        ,[总数] = sum(数量)
    from 
        your_table
    group by convert(char(6),datetime_col,112)