sql server 查询本周、本月所有天数的数据
查询本月所有的天数:
--本月所有的天数
select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) day from
(select substring(convert(varchar,GETDATE(),120),1,7)+'-01' day) t1,
(select number from MASTER..spt_values WHERE TYPE='P' AND number>=0 and number<=31) t2
where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like substring(convert(varchar,GETDATE(),120),1,7)+'%'
查询本周所有的天数:
select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 0),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 1),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 2),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 3),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 4),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 5),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 6),120)
示例:
如下图所示,有表MO_Orders,字段:BookTime,Number,Count,有如下数据。

查询出本周的每天的数据总和,语句如下:
with t as
(
select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 0),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 1),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 2),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 3),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 4),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 5),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 6),120)
)
select id=ROW_NUMBER()OVER(ORDER BY t1.date),
DATENAME(weekday,t1.date) as weekday,
t1.date,
Numbers=sum(isnull(t2.Number,0)),
Count=sum(isnull(t2.Count,0))
from t t1
left join
(
select substring(convert(varchar,BookTime,120),1,11) as BookTime,
Number,Count
from MO_Orders
where datediff(wk,BookTime-1,getdate()) = 0
)
t2
on t1.date= substring(convert(varchar,t2.BookTime,120),1,11)
group by t1.date
查询效果如下图,其中 weekday为星期,此图还需替换,稍后补上:
示例:如下图所示,有表: MO_Orders,字段:BookTime,Cost,Count 。
查询本月的所有数据总和(其中:total=Cost*Count)。

查询出本月的每天的数据总和,显示每天的,查询语句如下:
with t as
(
select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) date from
(select substring(convert(varchar,GETDATE(),120),1,7)+'-01' day) t1,
(select number from MASTER..spt_values WHERE TYPE='P' AND number>=0 and number<=31) t2
where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like substring(convert(varchar,GETDATE(),120),1,7)+'%'
)
select id=ROW_NUMBER()OVER(ORDER BY t1.date),
t1.date,
CostTotal=sum(isnull(t2.CostTotal,0))
from t t1
left join
(
select BookTime,sum(CostTotal) as CostTotal from
(
select substring(convert(varchar,BookTime,120),1,11) as BookTime,
Cost*Count as CostTotal
from MO_Orders
where datediff(month,BookTime,getdate()) = 0
) o group by BookTime
) t2
on t1.date= t2.BookTime
group by t1.date
查询结果如下图:
sql server 查询本周、本月所有天数的数据的更多相关文章
- SQL Server选取本周或上一周数据
有关SQL Server中有关周的数据查询主要思路来自下面这个语句 select getdate(), dateadd(wk, datediff(wk, 0, DateAdd(Day,-1,getda ...
- SQL SERVER 查询特定的前几条数据
1. 使用MS SQL Server 2008: 2. 数据库内容如下: insert into xuexi1 values('张三0', '数学', 98 ) insert into xuexi1 ...
- Sql Server 查询今天,昨天,近七天....数据
今天数据: 昨天数据: 7天内数据: 30天内数据: 本月数据: 本年数据: 查询今天是今年的第几天: select datepart(dayofyear,getDate()) 查询今天是本月的第几天 ...
- SQL Server查询第31到40条数据
大致分为两种情况:ID连续和ID不连续. 1.ID连续的情况: 2.ID不连续的情况: (1).两次对表查询,效率较低. ID from A) (2).外层查询没有对表A进行查询,效率提高. ID f ...
- sql server查询数据库的大小和各数据表的大小
查询出来的结果中各字段的详细说明参考MSDN资料:https://msdn.microsoft.com/zh-cn/library/ms188776.aspx 如果只是查询数据库的大小的话,直接使用以 ...
- SQL Server 获取本周,本月,本年等时间内记录
datediff(week,zy_time,getdate())=0 //查询本周 datediff(month,zy_time,getdate())=0 //查询本月 本季:select * fro ...
- SQLServer数据库之SQL Server 获取本周,本月,本年等时间内记录
本文主要向大家介绍了SQLServer数据库之SQL Server 获取本周,本月,本年等时间内记录,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助. datediff(we ...
- [转] 利用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 ...
随机推荐
- php框架的制作原理
php框架的制作原理 (2012-08-16 14:25:55) 转载▼ 标签: php框架制作 杂谈 分类: php index.php 主入口文件 <?php define('ISEXIS ...
- Java面试题大汇总
前言 关于赢在面试的Java题系列基本收集整理完成了,所有题目都是经过精心挑选的,很基础又考验求职者的基本功,应该说被面试到的几率很大.这里整理挑选出来供大家面试前拿来看一看,所有题目整理自网络,有一 ...
- bzoj 2124 等差子序列 (线段树维护hash)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1922 Solved: 714[Submit][Status][Discuss ...
- oracle exp导出加上过滤条件
exp username/password@dbname file='d:\hehe.dmp' tables=(%) query="""where UPDATE_DT ...
- 【刷题】BZOJ 4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...
- 20165218 《网络对抗技术》Exp1 逆向及Bof基础
Exp1 逆向及Bof基础 基础知识 1. NOP, JNE, JE, JMP, CMP汇编指令的机器码 指令 机器码 NOP NOP指令即"空指令",在x86的CPU中机器码为0 ...
- 【django基础之ORM,增删改查】
一.定义 1.什么是ORM? ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候, ...
- 设置Linux终端字体颜色
系统启动后,环境变量加载的顺序为:/etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → /etc/bashrc 想要修改 ...
- Web Service(下)
4.WSDL文档 <?xml version='1.0' encoding='UTF-8'?> <wsdl:definitions xmlns:xsd="http://ww ...
- Javascript/jQuery关于JSON或数组集合的几种循环方法
JavaScript遍历JSON或数组集合: /** * 根据json数据生成option树形控件 * 如果有children节点则自动生成树形数据 * @param {JSON} data * @p ...