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进行简单的数据显示,有时为了进行更加直观的数据显示,我们需要按照某个字段对列表进行 ...
随机推荐
- Oracle分析函数-keep(dense_rank first/last)
select * from criss_sales where dept_id = 'D02' order by sale_date ; 此时有个新需求,希望查看部门 D02 内,销售记录时间最早,销 ...
- MFC工程 重命名方法
C++ MFC工程 修改前的知识准备: 1.解决方案相关文件 AAA.sln文件和AAA.suo文件为MFC自动生成的解决方案文件,它包含当前解决方案中的工程信息,存储解决方案的设置. 2.工程相关文 ...
- SQL查看死锁+清理死锁
----查看sql死锁 CREATE procedure sp_who_lock as begin declare @spid int declare ...
- SpringBoot------整合MyBatis
1.添加pom.xml需要的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...
- CentOS上安装配置Ruby on Rails
0.install sublime editor(optional) ref:http://www.tecmint.com/install-sublime-text-editor-in-linux/ ...
- weblogic 整合cxf 报错:cannot create a secure XmlInputFactory
weblogic 整合cxf 报错:cannot create a secure XmlInputFactory ================================ ©Copyright ...
- 一个简易的netty udp服务端
netty号称java高性能网络库,为人帮忙中,研究了下,写了一个demo.反复调试,更改,局域网两个客户端同时for循环发10000个20字节的数据包,入库mysql,居然没丢. 思路,netty的 ...
- samba4.4security配置
security=share在新版中已经被废弃了把security = share改为 security = user map to guest = Bad User 就可以了 [global] wo ...
- 机器人学 —— 机器人感知(Mapping)
对于移动机器人来说,最吸引人的莫过于SLAM,堪称Moving Robot 皇冠上的明珠.Perception 服务于 SLAM,Motion Plan基于SLAM.SLAM在移动机器人整个问题框架中 ...
- PGP NO_PUBKEY
horizon@horizon-pc ~ $ sudo apt-get update Ign http://packages.linuxmint.com rebecca/upstream Transl ...