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 按年月日分组的更多相关文章

  1. [sql server、oracle] 分组取最大值最小值常用sql

    sqlserver2005前: --分组取最大最小常用sql--测试环境if OBJECT_ID('tb') is not null drop table tb;gocreate table tb(  ...

  2. SQL Server 根据日期分组、 根据时间段分组(每三个小时一组)

    所用数据表: 一.根据日期分组 1. 使用convert() 函数方式 --根据年月 ),CreatTime,)日期,COUNT(*) 次数,sum(Money)总数 from Orders ),Cr ...

  3. Sql Server中的分组

    1.Group by可以将相同的数据合并为一组,分组后的数据可以看成是一个临时的表,注意如果sql中有where条件,那么group by必须放在where之后. 2.GROUP BY子句中可以指定多 ...

  4. sql server group by 分组带sum avg求和需要注意的一点

    这是在写SQL语句遇到的一个sum  和group bu分组的问题

  5. sql server 中判断分组排序的使用示例

    现在需要查询一组数据,是对一列字段(column01)的数据分范围查询后分组排序: select (case when [column01] >0 AND [column01]<= 500 ...

  6. sql server 按照字段分组 重新设置组序号

      SELECT cpr.Id, cpr.CreateTime, cpr.Number FROM CarParkingRegistration cpr SELECT CONCAT(FORMAT(cpr ...

  7. SQL Server 2008 R2——分组取前几名

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

  8. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  9. SQL Server Reporting Service(SSRS) 第二篇 SSRS数据分组Parent Group

    SQL Server Reporting Service(SSRS) 第一篇 我的第一个SSRS例子默认使用Table进行简单的数据显示,有时为了进行更加直观的数据显示,我们需要按照某个字段对列表进行 ...

随机推荐

  1. 云游戏真的来了,这次的搅局者是 Google,云游戏平台搭建

    索尼.微软.任天堂和 Steam 等几家平台商的博弈. Google:云计算将会彻底改变我们的游戏方式 名为「Stadia」的全新游戏平台 和我们平时看到的索尼 PS4.微软 Xbox One 以及任 ...

  2. oracle表查询优化

    ORACLE有个高速缓冲的概念,这个高速缓冲就是存放执行过的SQL语句,那oracle在执行sql语句的时候要做很多工作,例如解析sql语句,估算索引利用率,绑定变量,读取数据块等等这些操作.假设高速 ...

  3. SpringBoot系统列 3 - 多线程数据处理(ThreadPoolTaskExecutor、DruidDataSource)

    在上篇文章的基础上进行改造: package com.hello.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; impo ...

  4. Angular4学习笔记-目录汇总

    Angular4学习笔记(一)-环境搭建 Angular4学习笔记(二)-在WebStorm中启动项目 Angular4学习笔记(三)- 路由 Angular4学习笔记(四)- 依赖注入 Angula ...

  5. js返回上一页并刷新、返回上一页、自动刷新页面

    一.返回上一页并刷新 <a href="javascript:" onclick="self.location=document.referrer;"&g ...

  6. 利用Python半自动化生成Nessus报告

    0x01 前言 Nessus是一个功能强大而又易于使用的远程安全扫描器,Nessus对个人用户是免费的,只需要在官方网站上填邮箱,立马就能收到注册号了,对应商业用户是收费的.当然,个人用户是有16个I ...

  7. Spring的事务管理基础知识

    1.数据库事务基础知识     1)数据库事务有严格的定义,它必须同时满足4个特性:原子性(Atomic).一致性(Consistency).隔离性(Isolation)和持久性(Durability ...

  8. linux 系统全盘恢复

    恢复备份 一.准备 1. 从 u盘启动,进入 live-cd 系统,配置好网络和镜像源,更新一下仓库的数据库. sudo pacman -Syy 2. 安装 timeshift 这个软件. sudo ...

  9. Win10上使用VS2015编译Caffe2

    Caffe2的官网:https://caffe2.ai/ 1.下载.安装及相关准备 在Caffe2的官网点击"Get Started",即进入安装说明页面.官方还未提供编译好的bi ...

  10. PHP(Dom操作)

    jsDOM操作组成: ECMAscript:语法核心 BOM:浏览器对象模型 window:窗口 open close 定时器 dingsh history:历史记录 go back location ...