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. [MySQL复制异常]'Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.'

    MySQL复制错误]Last_Errno: 1666 Last_Error: Error executing row event: 'Cannot execute statement: imposs ...

  2. Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题

    D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

  3. android调用系统相机拍照并保存在本地

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  4. MySQL高可用之MHA的搭建 转

     http://www.cnblogs.com/muhu/p/4045780.html http://www.cnblogs.com/gomysql/p/3675429.html http://www ...

  5. ls 显示目录和文件的技巧

    摘自 http://hi.baidu.com/zaoyuan1217/blog/item/fd69575660366b4fd10906b9.html 要列出当前目录下所有的文件名和目录名直接使用ls命 ...

  6. linux modprobe命令参数及用法详解--linux加载模块命令

    转:http://www.linuxso.com/command/modprobe.html modprobe(module probe) 功能说明:自动处理可载入模块. 语 法:modprobe [ ...

  7. LeetCode49 Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  8. InAction-MR的topK

    本来只是想拿搜狗的数据练练手的,却无意踏足MR的topK问题.经过几番波折,虽然现在看起来很简单,但是摸爬滚打中也学到了不少 数据是搜狗实验室下的搜索日志,格式大概为: 00:00:00 298219 ...

  9. Sql 之 sql中的强制类型转换

    1. convert(数据类型, 字段名) convert(datetime, startDate) 2. cast(字段名 as 数据类型) ,))

  10. 【Shell脚本学习5】第一个Shell脚本

    打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell 脚本,扩展名就用php好了. 输入一些代码: #!/bin/bash ...