sql server 查询本年的每个月的数据
一、以一行数据的形式,显示本年的12月的数据,本示例以2017年为例,根据CreateDate字段判断,计算总和,查询语句如下:
select sum(case when datepart(month,CreateDate)=1 then 1 else 0 end) as '1月',
sum(case when datepart(month,CreateDate)=2 then 1 else 0 end) as '2月',
sum(case when datepart(month,CreateDate)=3 then 1 else 0 end) as '3月',
sum(case when datepart(month,CreateDate)=4 then 1 else 0 end) as '4月',
sum(case when datepart(month,CreateDate)=5 then 1 else 0 end) as '5月',
sum(case when datepart(month,CreateDate)=6 then 1 else 0 end) as '6月',
sum(case when datepart(month,CreateDate)=7 then 1 else 0 end) as '7月',
sum(case when datepart(month,CreateDate)=8 then 1 else 0 end) as '8月',
sum(case when datepart(month,CreateDate)=9 then 1 else 0 end) as '9月',
sum(case when datepart(month,CreateDate)=10 then 1 else 0 end) as '10月',
sum(case when datepart(month,CreateDate)=11 then 1 else 0 end) as '11月',
sum(case when datepart(month,CreateDate)=12 then 1 else 0 end) as '12月'
from MO_Members
where datepart(year,CreateDate)=''
查询结果如下:
二、根据当前日期,以列的数据形式,显示本年的12个月的数据,查询语句如下:
select date=convert(varchar(10),dateadd(month, 0,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,1,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,2,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,3,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,4,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,5,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,6,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,7,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,8,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,9,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,10,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
union all select date=convert(varchar(10),dateadd(month,11,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120)
查询结果如下:
二、具体应用示例:以2017为例,查询语句如下:
with t as
(
select date=substring(convert(varchar(10),dateadd(month, 0,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,1,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,2,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,3,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,4,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,5,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,6,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,7,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,8,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,9,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,10,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
union all select date=substring(convert(varchar(10),dateadd(month,11,convert(varchar(10),DATEADD(year, DATEDIFF(year,0,getdate()), 0),120)),120),6,2)
)
select id=ROW_NUMBER()OVER(ORDER BY t1.date),
date=t1.date+'月',
Counts=sum(isnull(t2.counts,0))
from t t1
left join
(
select substring(convert(varchar,CreateDate,120),6,2) as CreateDate,count(*) as counts
from MO_Members
where datepart(year,CreateDate)=''
group by substring(convert(varchar,CreateDate,120),6,2)
) t2
on t1.date= CreateDate
group by t1.date
查询结果如下:
sql server 查询本年的每个月的数据的更多相关文章
- sql server 查询当前月份日期列表数据
SELECT ), ,) AS every_time, --日期 ,getdate())) ) AS Weekd --星期几 FROM master..spt_values n WHERE n.typ ...
- MS Sql Server 查询数据库中所有表数据量
方法一: SELECT a.name,b.rows FROM sysobjects a INNER JOIN sysindexes b ON a.id=b.id ,) AND a.Type='u' O ...
- 学习如何看懂SQL Server执行计划(一)——数据查询篇
一.数据查询部分 1. 看到执行计划有两种方式,对sql语句按Ctrl+L,或按Ctrl+M打开显示执行计划窗口每次执行sql都会显示出相应的执行计划 2. 执行计划的图表是从右向左看的 3. SQL ...
- 面试1 SQL SERVER 查询第20行到30之间的数据
SQL SERVER 查询第20行到30之间的数据 1.先查询前20行的ID,后查询除去20条记录的前10条记录 SELECT TOP 10 * FROM tbBank WHERE BankID NO ...
- 用SQL Server查询所有数据并显示
利用SQL Server查询数据,并把所有数据显示在页面上. "%> <%@page import="jdbc.sqlServer"%> <%@p ...
- [转] 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能
首先需要说明的是这篇文章的内容并不是如何调节SQL Server查询性能的(有关这方面的内容能写一本书),而是如何在SQL Server查询性能的调节中利用SET STATISTICS IO和SET ...
- SQL SERVER 查询性能优化——分析事务与锁(五)
SQL SERVER 查询性能优化——分析事务与锁(一) SQL SERVER 查询性能优化——分析事务与锁(二) SQL SERVER 查询性能优化——分析事务与锁(三) 上接SQL SERVER ...
- SQL Server ---(CDC)监控表数据(转译)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现过程(Realization) 补充说明(Addon) 参考文献(References) ...
- SQL Server 查询性能优化 相关文章
来自: SQL Server 查询性能优化——堆表.碎片与索引(一) SQL Server 查询性能优化——堆表.碎片与索引(二) SQL Server 查询性能优化——覆盖索引(一) SQL Ser ...
随机推荐
- [知乎]老狼:深入PCI与PCIe之二:软件篇
深入PCI与PCIe之二:软件篇 https://zhuanlan.zhihu.com/p/26244141 我们前一篇文章(深入PCI与PCIe之一:硬件篇 - 知乎专栏)介绍了PCI和PCIe的硬 ...
- how to get iframe dom in js
how to get iframe dom in js https://stackoverflow.com/questions/3999101/get-iframes-document-from-ja ...
- delphi的ActionToolBar控件的使用 [问题点数:50分]
delphi那些默认图标在哪里就是那些Text,Label,Checkbox,显示在palette上面的那些图标. 上面的按钮是在ActionManager中添加的,让后拖到ActionToolBar ...
- 深入理解JAVA虚拟机阅读笔记6——线程安全与锁优化
线程安全:如果一个对象可以安全的被多个线程同时使用,那它就是线程安全的. 一.Java中的线程安全 1.不可变 不可变的对象一定是线程安全的.String.枚举类型.java.lang.Number的 ...
- 环形buffer缓冲区
#include <stdio.h> #include <string.h> #include <malloc.h> struct CircleBuf { char ...
- Codeforces 893F(主席树+dfs序)
在子树内和距离不超过k是一个二维限制,容易想到主席树,但主席树显然没法查最小值,因为不满足区间可减.kdtree和二维线段树可以干这事,但肯定会T飞.但事实上我们的问题有一个特殊性:对某个点x,查询其 ...
- Java IO流学习总结 - BIO
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据 ...
- The Best Path HDU - 5883(欧拉回路 && 欧拉路径)
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版
Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...
- 【THUSC2017】座位
题目背景 班级聚会的时候,班主任为了方便管理,规定吃饭的时候同一个寝室的同学必须坐在一起;但是吃完饭后,到了娱乐时间,喜欢不同游戏的同学会聚到一起;在这个过程中就涉及到了座位分配的问题. 题目描述 有 ...