惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
MyScale Blog
MyScale Blog
Jina AI
Jina AI
B
Blog
Microsoft Security Blog
Microsoft Security Blog
T
Troy Hunt's Blog
博客园_首页
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
T
Tenable Blog
B
Blog RSS Feed
S
Securelist
T
Threat Research - Cisco Blogs
P
Privacy International News Feed
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
AWS News Blog
AWS News Blog
V
V2EX
宝玉的分享
宝玉的分享
J
Java Code Geeks
小众软件
小众软件
Spread Privacy
Spread Privacy
腾讯CDC
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
V
Visual Studio Blog
The Hacker News
The Hacker News
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Know Your Adversary
Know Your Adversary
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
C
Check Point Blog
Webroot Blog
Webroot Blog
D
DataBreaches.Net
Cloudbric
Cloudbric
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家

博客园 - blog of mb459

项目管理实战之团队管理 (转自:zhuweisky) 刘韧专访陈一舟:回顾ChinaRen时代 (ZT) 谈我对攻读计算机研究生的看法 - blog of mb459 个性主张:玩转你的86400秒 全世界所有程序员都会犯的错误-蔡学镛 变态级JAVA程序员面试32问(附答案)(转载) 互联网创业迎来回潮 风投开始第2波网络掘金 认识.NET的集合(转) 学习:怎样才是一个真正的DBA? sql 面试中的问题 IT从业人员必看的10大论坛 你为什么离开你以前的公司?(整理自csdn社区) 怎样从一名程序员过度到项目经理(整理自csdn论坛) 一篇不错的讲解Java异常的文章 新技术倍出 谁最可能挑战Java开发优势 我喜欢的10款Freeware 55种网页常用小技巧(转载) 使用Vs2005打造简单分页浏览器(1) C#中的类型转换(转载)
一个关于group by的sql
blog of mb459 · 2005-12-10 · via 博客园 - blog of mb459

提问:
         一个表有三个字段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字段和
回答:
    相应sql如下:

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

其实就用了一个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)-11when -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)-11when -1 then 11 else 13 end
8 else datepart(month,dt) end

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

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

最后,明白group by 后面不仅可以跟字段名就可以了。
------------------------------------------------------------------
再问再续:
1 按照旬统计

2按 年+旬 分组统计
 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 统计
 from abc group by
 datename(year,dt)+datename(month,dt)+case (datepart(day,dt)-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end