sql server 按年月日分组
sql server 按年月日分组
-----------------------------------------------
--author:yangjinwang
--date:2017-03-30
--do:根据活动查询 每个奖品类的发放数量
-----------------------------------------------
create proc GetWinningInfoTypeCountByTimeActivity
@CreateTimeStart datetime =null, --开始时间
@CreateTimeEnd datetime =null, --结束时间
@TimeType varchar(20)='m', --查询维度,年月日
@ActivityId int=null --活动ID
as
begin
if(@TimeType='y')
begin
select
c.id as '活动ID',
c.Name as '活动名称',
cast(datepart(YEAR,a.CreateTime) as varchar(4)) as '日期',
b.Title as '奖品类别'
,COUNT(a.Id) as '中奖数量'
from WinningInfo a
left join PrizesInfo b on a.PrizesId=b.Id
left join ActivityInfo c on b.ActivityId=c.Id
where a.ActivityId=@ActivityId
and (a.CreateTime>=@CreateTimeStart or @CreateTimeStart is null)
and (a.CreateTime<=@CreateTimeEnd or @CreateTimeEnd is null)
group by c.id,c.Name ,
cast(datepart(YEAR,a.CreateTime) as varchar(4)) ,
b.Title
order by 日期
end
else if(@TimeType='m')
begin
select
c.id as '活动ID',
c.Name as '活动名称',
cast(datepart(YEAR,a.CreateTime) as varchar(4))+'-'+RIGHT(''+CAST(MONTH(a.CreateTime) AS VARCHAR(2)),2) as '日期',
b.Title as '奖品类别'
,COUNT(a.Id) as '中奖数量'
from WinningInfo a
left join PrizesInfo b on a.PrizesId=b.Id
left join ActivityInfo c on b.ActivityId=c.Id
where a.ActivityId=@ActivityId
and (a.CreateTime>=@CreateTimeStart or @CreateTimeStart is null)
and (a.CreateTime<=@CreateTimeEnd or @CreateTimeEnd is null)
group by c.id,c.Name ,
cast(datepart(YEAR,a.CreateTime) as varchar(4))+'-'+RIGHT(''+CAST(MONTH(a.CreateTime) AS VARCHAR(2)),2) ,
b.Title
order by 日期
end
else
begin
select
c.id as '活动ID',
c.Name as '活动名称',
cast(datepart(YEAR,a.CreateTime) as varchar(4))+'-'+RIGHT(''+CAST(MONTH(a.CreateTime) AS VARCHAR(2)),2)+'-'+RIGHT(''+CAST(day(a.CreateTime) AS VARCHAR(2)),2) as '日期',
b.Title as '奖品类别'
,COUNT(a.Id) as '中奖数量'
from WinningInfo a
left join PrizesInfo b on a.PrizesId=b.Id
left join ActivityInfo c on b.ActivityId=c.Id
where a.ActivityId=@ActivityId
and (a.CreateTime>=@CreateTimeStart or @CreateTimeStart is null)
and (a.CreateTime<=@CreateTimeEnd or @CreateTimeEnd is null)
group by c.id,c.Name ,
cast(datepart(YEAR,a.CreateTime) as varchar(4))+'-'+RIGHT(''+CAST(MONTH(a.CreateTime) AS VARCHAR(2)),2)+'-'+RIGHT(''+CAST(day(a.CreateTime) AS VARCHAR(2)),2) ,
b.Title
order by 日期
end
end
年,月,日,季,旬
--还可以这样
--年
select datepart(YEAR,getdate())
select datepart(yyyy,getdate())
select datepart(yy,getdate())
--月
select datepart(MONTH,getdate())
select datepart(mm,getdate())
select datepart(m,getdate())
--日
select datepart(dd,getdate())
--1年中的第多少天
select datepart(dy,getdate())
--季度
select datepart(qq,getdate()) select datepart(qq,'2017-07-01')
--1年中的第多少周
select datepart(wk,getdate())
--星期 --因为从周日算第一天的,星期4,计算结果是5
select datepart(dw,getdate())-1 SELECT CONVERT(VARCHAR(10),GETDATE(),120) --2015-07-13
SELECT CONVERT(VARCHAR(10),GETDATE(),101) --07/13/2015 --按日分组:
select convert(nvarchar(10),GETDATE(),120)
--按年月分组:
select cast(datepart(YEAR,GETDATE()) as varchar(4))+'-'+RIGHT(''+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2)
--按年分组
select DATEPART(year,GETDATE()) --按旬分组
select case (datepart(day,GETDATE())-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end as 旬,
sum(1) as 统计
from 表A group by
case (datepart(day,dt)-1)/10 when 0 then '上旬' when 1 then '中旬' else '下旬' end ------------------------------------------------------------------------------------
另一种Convert 年月日分组方式
--按日
select convert(nvarchar(10),GETDATE(),120)
--按月
select convert(nvarchar(7),GETDATE(),120)
--按年
select convert(nvarchar(4),GETDATE(),120)
sql server 按年月日分组的更多相关文章
- [sql server、oracle] 分组取最大值最小值常用sql
sqlserver2005前: --分组取最大最小常用sql--测试环境if OBJECT_ID('tb') is not null drop table tb;gocreate table tb( ...
- SQL Server 根据日期分组、 根据时间段分组(每三个小时一组)
所用数据表: 一.根据日期分组 1. 使用convert() 函数方式 --根据年月 ),CreatTime,)日期,COUNT(*) 次数,sum(Money)总数 from Orders ),Cr ...
- Sql Server中的分组
1.Group by可以将相同的数据合并为一组,分组后的数据可以看成是一个临时的表,注意如果sql中有where条件,那么group by必须放在where之后. 2.GROUP BY子句中可以指定多 ...
- sql server group by 分组带sum avg求和需要注意的一点
这是在写SQL语句遇到的一个sum 和group bu分组的问题
- sql server 中判断分组排序的使用示例
现在需要查询一组数据,是对一列字段(column01)的数据分范围查询后分组排序: select (case when [column01] >0 AND [column01]<= 500 ...
- sql server 按照字段分组 重新设置组序号
SELECT cpr.Id, cpr.CreateTime, cpr.Number FROM CarParkingRegistration cpr SELECT CONCAT(FORMAT(cpr ...
- SQL Server 2008 R2——分组取前几名
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
- SQL Server Reporting Service(SSRS) 第二篇 SSRS数据分组Parent Group
SQL Server Reporting Service(SSRS) 第一篇 我的第一个SSRS例子默认使用Table进行简单的数据显示,有时为了进行更加直观的数据显示,我们需要按照某个字段对列表进行 ...
随机推荐
- Linux下用gSOAP开发Web Service服务端和客户端程序
网上本有一篇流传甚广的C版本的,我参考来实现,发现有不少问题,现在根据自己的开发经验将其修改,使用无误:另外,补充同样功能的C++版本,我想这个应该更有用,因为能用C++,当然好过受限于C. 1.gS ...
- msm audio platform 驱动代码跟踪
sound/soc/soc-core.c static int __init snd_soc_init(void) { #ifdef CONFIG_DEBUG_FS snd_soc_debugfs_r ...
- 【分享】Asp.net Core相关教程及开源项目
入门 全新的ASP.NET: https://www.cnblogs.com/Leo_wl/p/5654828.html 在IIS上部署你的ASP.NET Core项目: https://www.c ...
- myeclipse及Eclipse中.classpath、.project、.settings、.mymetadata(myeclipse特有)介绍
引言 今天在创建java项目的时候遇到了很多的错误,在解决的过程中遇到了一些根本不知道什么作用的文件,然后按照网上的一些做法可以将问题解决,但是这也说明我们在学习的时候很多基础和细节的地方是我们薄弱的 ...
- python学习笔记(五)
面向对象方法 元组的二义性:不明确参数代表的含义 circle=(2,4,6) def distance_from_origin(x,y): return "返回x,y坐标" de ...
- C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...
- Elasticsearch学习之有用博客
推荐阅读:1.阿里:https://elasticsearch.cn/article/61712.滴滴:http://t.cn/EUNLkNU3.腾讯:http://t.cn/E4y9ylL4.携程: ...
- B - Assignment
Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...
- php跨域发送请求原理以及同步异步问题
<script async type="text/javascript" src="http://lisi.com/data.php?flag=1"> ...
- 15、js 原生基础总结
Day1 一.什么是JS? ==基于对象==和==事件驱动==的客户端脚本语言 二.哪一年?哪个公司?谁?第一个名字是什么? 1995,NetScape(网景公司),布兰登(Brendan Eic ...