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('00'+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('00'+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('00'+CAST(MONTH(a.CreateTime) AS VARCHAR(2)),2)+'-'+RIGHT('00'+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('00'+CAST(MONTH(a.CreateTime) AS VARCHAR(2)),2)+'-'+RIGHT('00'+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('00'+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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM