如何在sql server中只获得12个月的当前年份?

时间:2017-08-23 10:18:13

标签: sql-server

我为获取12个月的名称创建一个查询,并计算当前年份的12个月数据。但我的问题是我面临的问题。现在我当前年度当前月份是08.所以我的查询01到08个月计数正确。但是在09,10,11,12之后这个4个月获得2016年数据。我想在这里只获得当月的当月数据。

这是我的查询=>

SELECT FORMAT(dateadd(MM, T.i, getdate()),'MMM-yy') AS DateColumn, case when TotalCount is null then 0  else TotalCount end as TotalCount
    FROM (VALUES (-11),(-10),(-9),(-8),(-7),(-6), (-5), (-4), (-3), (-2), (-1), (0)) AS T(i)
    OUTER APPLY
    (
       SELECT distinct Count(datepart(MM,cc.InsertDateTime)) over (partition by datepart(MM,cc.InsertDateTime)) AS TotalCount
           FROM User cc
           where Datediff(DAY,DATEADD(mm, DATEDIFF(mm, 0, cc.InsertDateTime), 0), dateadd(month, T.i, DATEADD(mm, DATEDIFF(MM, 0, GETDATE()), 0))) = 0 and IsLogin = 1
    ) uf
     order by DATEPART(MM,convert(datetime,FORMAT(dateadd(MM, T.i, getdate()),'MMM') +'01 2017',110))

这是我的数据=>

UserId | IsLogin | InsertDateTime
-------+---------+---------------------------
  1         1        2017-08-22 16:12:46.713
  2         1        2017-08-22 16:50:55.789
  3         1        2017-07-10 16:12:46.713
  4         1        2017-07-10 16:12:46.713
  5         1        2017-06-21 16:12:46.713
  6         1        2017-06-04 16:12:46.713
  7         1        2017-06-05 16:12:46.713
  8         1        2016-09-05 16:12:46.713
  9         1        2016-09-05 16:12:46.713
  10         1        2016-10-05 16:12:46.713
  11         1        2016-11-05 16:12:46.713

这是我目前的输出:

DateColumn  |  TotalCount
Jan-17         0
Feb-17         0
Mar-17         0
Apr-17         0
May-17         0 
Jun-17         3
Jul-17         2
Aug-17         2 
Sep-16         2
Oct-16         1
Nov-16         1 
Dec-16         1 

我的预期输出应为:

 DateColumn  |  TotalCount
  Jan-17         0
  Feb-17         0
  Mar-17         0
  Apr-17         0
  May-17         0 
  Jun-17         3
  Jul-17         2
  Aug-17         2 
  Sep-17         0
  Oct-17         0
  Nov-17         0 
  Dec-17         0

我的查询我也在2016年获得数据,但我不想这样做。

3 个答案:

答案 0 :(得分:3)

您可以通过以下代码替换前两行代码来实现此目的 -

declare @CurMon int
SELECT  @CurMon = month(getdate())
select @CurMon
SELECT FORMAT(dateadd(MM, T.i, getdate()),'MMM-yy') AS DateColumn, case when TotalCount is null then 0  else TotalCount end as TotalCount
FROM (VALUES (11-@CurMon),(10-@CurMon),(9-@CurMon),(8-@CurMon),(7-@CurMon),(6-@CurMon), (5-@CurMon), (4-@CurMon), (3-@CurMon), (2-@CurMon), (1-@CurMon), (0-@CurMon)) AS T(i)

答案 1 :(得分:1)

这样的事情:

DECLARE @DataSource TABLE
(
    [UserId] TINYINT
   ,[IsLogin] TINYINT
   ,[InsertDateTime] DATETIME2
);

INSERT INTO @DataSource ([UserId], [IsLogin], [InsertDateTime])
VALUES   (1, 1, '2017-08-22 16:12:46.713')
        ,(2, 1, '2017-08-22 16:50:55.789')
        ,(3, 1, '2017-07-10 16:12:46.713')
        ,(4, 1, '2017-07-10 16:12:46.713')
        ,(5, 1, '2017-06-21 16:12:46.713')
        ,(6, 1, '2017-06-04 16:12:46.713')
        ,(7, 1, '2017-06-05 16:12:46.713')
        ,(8, 1, '2016-09-05 16:12:46.713')
        ,(9, 1, '2016-09-05 16:12:46.713')
        ,(10, 1, '2016-10-05 16:12:46.713')
        ,(11, 1, '2016-11-05 16:12:46.713');

WITH PeriodsRange ([Year], [Month]) AS
(
    SELECT *
    FROM 
    (
        VALUES (2017, 01)
              ,(2017, 02)
              ,(2017, 03)
              ,(2017, 04)
              ,(2017, 05)
              ,(2017, 06)
              ,(2017, 07)
              ,(2017, 08)
              ,(2017, 09)
              ,(2017, 10)
              ,(2017, 11)
              ,(2017, 12)
    ) DS ([Year], [Month])
)
SELECT [Year], [Month]
      ,SUM(IIF(DS.[InsertDateTime] IS NULL, 0, 1))
FROM PeriodsRange PR
LEFT JOIN @DataSource DS
    ON PR.[Year] = YEAR(DS.[InsertDateTime])
    AND PR.[Month] = MONTH(DS.[InsertDateTime])
GROUP BY [Year], [Month];

enter image description here

答案 2 :(得分:1)

这也可能有效:

SELECT  LEFT(DateName(M, DateAdd(M, Mnth, -1)),3)+ '-' + RIGHT(yr,2) [display],COUNT(yt.AnyNotNullColumn)
FROM    (SELECT YEAR(GETDATE()) [yr]) AS T
        --CROSS APPLY(SELECT number FROM master..spt_values WHERE type = 'P' AND number between 1 AND 12) AS T1(Mnth)
        --OR you can hardcode the values
        CROSS APPLY(values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) AS T1(Mnth)
        LEFT JOIN YourTable yt
            ON YEAR(yt.YourDateCol) = t.yr
               AND MONTH(yr.YourDateCol) = t1.Mnth
GROUP   BY LEFT(DateName(M, DateAdd(M, Mnth, -1)),3)+ '-' + RIGHT(yr,2)