SQLSERVER中按年月分组

一个表有三个字段id,dt,d  分别存放id,时间,数值  
id    dt    d 
1 2004-08-11 12:12:00.000 9  
2 2005-09-11 12:08:00.000 2  
3 2005-08-11 12:12:00.000 6  
4 2005-09-11 12:12:00.000 10  
5 2005-08-11 12:12:00.000 0  
要求按照时间里的月份分组求d字段和

 1 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[abc]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
2 drop table [dbo].[abc]
3 GO
4
5 CREATE TABLE [dbo].[abc] (
6 [id] [int] NOT NULL ,
7 [dt] [datetime] NULL ,
8 [d] [int] NULL
9 ) ON [PRIMARY]
10 GO
11
12 insert into abc (id,dt,d) values(1,'2004-08-11 12:12:00',9)
13 insert into abc (id,dt,d) values(2,'2005-09-11 12:8:00',2)
14 insert into abc (id,dt,d) values(3,'2005-08-11 12:12:00',6)
15 insert into abc (id,dt,d) values(4,'2005-09-11 12:12:00',10)
16 insert into abc (id,dt,d) values(5,'2005-08-11 12:12:00',0)
17 insert into abc (id,dt,d) values(6,'2004-11-2 12:12:00',4)
18 insert into abc (id,dt,d) values(7,'2004-11-10 12:12:00',4)
19 insert into abc (id,dt,d) values(8,'2004-11-30 12:12:00',4)
20
21 select * from abc
22 select datepart(month,dt)as 月份,sum(d) as 合计 from abc group by datepart(month,dt)

其实就用了一个DATEPART函数 
引申一下:如果统计1,2,3,4,5,6,7,8,9,10,11月上旬,11月中下旬,12月的怎么办?
可以这样:

1 select case datepart(month,dt)
2 when 11 then case sign(datepart(day,dt)-11) when -1 then 11 else 13 end
3 else datepart(month,dt) end as 月份,
4 sum(d) as 统计
5 from abc group by
6 case datepart(month,dt)
7 when 11 then case sign(datepart(day,dt)-11) when -1 then 11 else 13 end
8 else datepart(month,dt) end

再引申,如果统计把年月作为分组统计的依据可以这样:

1 select datename(year,dt)+datename(month,dt)as 年月 ,sum(d) as 统计 from abc group by datename(year,dt)+datename(month,dt)

最后,明白group by 后面不仅可以跟字段名就可以了。

再问再续:
1、按照旬统计

1 select case (datepart(day,dt)-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end as 旬,
2 sum(d) as 统计
3 from abc group by
4 case (datepart(day,dt)-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end

2、按 年+旬 分组统计

1 select datename(year,dt)+datename(month,dt)+case (datepart(day,dt)-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end as 日期, sum(d) as 统计
2 from abc
3 group by datename(year,dt)+datename(month,dt)+case (datepart(day,dt)-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end

SQLSERVER中按年月分组的更多相关文章

  1. SQLSERVER中的ALL、PERCENT、CUBE关键字、ROLLUP关键字和GROUPING函数

    SQLSERVER中的ALL.PERCENT.CUBE关键字.ROLLUP关键字和GROUPING函数 先来创建一个测试表 USE [tempdb] GO )) GO INSERT INTO [#te ...

  2. SQLSERVER中的假脱机spool

    SQLSERVER中的假脱机spool 我发现网上对于假脱机的解释都非常零散,究竟假脱机是什么? 这几天在家里研究了一下,收集了很多网上的资料 假脱机是中文的翻译,而英文的名字叫做 spool 在徐老 ...

  3. [转]在SqlServer 中解析JSON数据

      在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...

  4. SqlServer中Sql语句的逻辑执行顺序

    准备数据 Sql脚本如下,两张表,一张客户表Customers只包含customerid和city字段,一张订单表Orders包含orderid和customerid(关联Customers的cust ...

  5. 在SQL Server 2012中如何使用分组集

    作者:Itzik Ben-Gan  翻译:张洪举 此文摘自作者的<Microsoft SQL Server 2012 T-SQL基础>. 分组集就是你据以分组的一个属性集.传统上,SQL中 ...

  6. SQLServer 中实现类似MySQL中的group_concat函数的功能

    SQLServer中没有MySQL中的group_concat函数,可以把分组的数据连接在一起. 后在网上查找,找到了可以实现此功能的方法,特此记录下. SELECT a, stuff((SELECT ...

  7. SqlServer中怎么删除重复的记录(表中没有id)

    SqlServer中怎么删除重复的记录(表中没有id) 其实我在别的网址也查到过删除重复的记录,不知道我是我SqlServer2012版本太低还是啥原因 delete from scwhere (c# ...

  8. 我是如何在SQLServer中处理每天四亿三千万记录的

    首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...

  9. SQLSERVER中NULL位图的作用

    SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...

随机推荐

  1. EasyUI-Tab 标签添加右键菜单

    在网上看了很多demo 自己实现了一个效果如下 <!doctype html> <html> <head> <meta http-equiv="co ...

  2. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  3. OSG 实现跟随节点的相机(转)

      本章教程将继续使用回调和节点路径(NodePath)来检索节点的世界坐标. 本章目标: 在一个典型的仿真过程中,用户可能需要从场景中的各种车辆和人物里选择一个进行跟随.本章将介绍一种将摄像机“依附 ...

  4. [Angular2 Router] Build Angular 2 Navigation with routerLink

    Angular 2 navigation is configured using the routerLink directive. The routerLink directive behaves ...

  5. percona-xtrabackup 文档

    https://www.percona.com/doc/percona-xtrabackup/2.4/index.html

  6. VS环境下搭建自己NuGet服务器

    一.NuGet服务端的搭建 环境:.NET 4.5 + VS2015 + NuGet.Server 2.10.1 1.建一个空的Web项目,取名叫NuGetServer 2.通过NuGet安装NuGe ...

  7. A SQLite client library written in Modern C++

    smartdb是一个纯c++11开发,header-only,简洁高效的sqlite封装库. github地址:https://github.com/chxuan/smartdb,如果您觉得不错,请不 ...

  8. iOS runtime 运行时( 二 )

    我们在编程过程中,如果使用到了runtime(运行时),我们几乎都是想动态的改变这个类的信息,包括方法,属性,balabala的,并且获得这个类的一些信息,等等,下面我们就来看看怎么通过runtime ...

  9. 属性通知之INotifyPropertyChanged

    为什么后台绑定的值改变了前台不发生变化了? 针对这个初学者很容易弄错的问题,这里介绍一下INotifyPropertyChanged的用法 INotifyPropertyChanged:用于绑定属性更 ...

  10. A+B Coming

    Problem Description Many classmates said to me that A+B is must needs.If you can’t AC this problem, ...