获取12个月的当前年份,其中包含SQL Server中的月份计数数据?

时间:2017-08-23 06:26:19

标签: sql-server

我正在创建一个查询以获取12个月的名称,我还想要UserLogin的月份计数。我想在条形图中显示月份明智的报告。所以我需要在SQL Server中创建一个查询。但是根据我的询问,我没有达到预期的12个月和计数。我整个月总是得1分。

这是我的查询,请你帮我错误=>

SELECT 
    FORMAT(dateadd(MM, T.i, getdate()),'MMMM') AS DateColumn, 
    uf.TotalCount
FROM 
    (VALUES (-11),(-10),(-9),(-8),(-7),(-6), (-5), (-4), (-3), (-2), (-1), (0)) AS T(i)
OUTER APPLY
    (SELECT 
         COUNT(datepart(MM, InsertDateTime)) AS TotalCount
     FROM 
         User 
     WHERE 
         DATEDIFF(mm, InsertDateTime, DATEADD(day, T.i, GETDATE())) = 0  
         AND IsLogin  = 1) uf

这是我在表格中的数据:

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
  ------------+--------------
  September        1
  October          1
  November         1  
  December         1 
  January          1
  February         1 
  March            1
  April            1 
  May              1
  June             1 
  July             1 
  August           1

我的预期输出应为:

DateColumn  |  TotalCount
------------+--------------
January          0
February         0 
March            0
April            0 
May              0
June             3 
July             2 
August           2 
September        0
October          0
November         0  
December         0

请告诉我查询中的问题在哪里。谢谢

4 个答案:

答案 0 :(得分:1)

你能尝试这样的事吗? Live Demo

DECLARE @StartDate SMALLDATETIME, @EndDate SMALLDATETIME;

SELECT @StartDate = '20170101', @EndDate = '20171231';

;WITH d(d) AS 
(
  SELECT DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate), 0))
  FROM ( SELECT TOP (DATEDIFF(MONTH, @StartDate, @EndDate) + 1) 
    n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
    FROM sys.all_objects ORDER BY [object_id] ) AS n
)
SELECT 
  [Month]    = DATENAME(MONTH, d.d),   
  OrderCount = COUNT(o.UserId) 
FROM d LEFT OUTER JOIN Table1 AS o
  ON o.InsertDateTime >= d.d
  AND o.InsertDateTime < DATEADD(MONTH, 1, d.d)
GROUP BY d.d
ORDER BY d.d;

enter image description here

答案 1 :(得分:1)

在您的代码中进行一些修改后

SELECT  FORMAT(dateadd(MM, T.i, getdate()),'MMMM') 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,InsertDateTime)) AS TotalCount
           FROM user1 
           WHERE Datediff(mm,InsertDateTime, dateadd(month, T.i, getdate())) = 0  and IsLogin  = 1
    ) uf
    order by DATEPART(MM,convert(datetime,FORMAT(dateadd(MM, T.i, getdate()),'MMMM') +'01 2017',110))

输出 -

DateColumn  TotalCount
January     0
February    0
March       0
April       0
May         0
June        3
July        2
August      2
September   0
October     0
November    0
December    0

答案 2 :(得分:0)

请修改您的查询,如下所示:

SELECT *
FROM (
SELECT FORMAT(dateadd(MM, T.i, getdate()), 'MMMM') AS DateColumn --, uf.TotalCount
FROM (
    VALUES (- 11)
        ,(- 10)
        ,(- 9)
        ,(- 8)
        ,(- 7)
        ,(- 6)
        ,(- 5)
        ,(- 4)
        ,(- 3)
        ,(- 2)
        ,(- 1)
        ,(0)
    ) AS T(i)
) A
LEFT JOIN (
SELECT datename(month, calcdate) datecol
    ,count(*) #trxns
FROM EmployeeMTDResults
WHERE calcdate BETWEEN '2017-03-01'
        AND getdate()
GROUP BY datename(month, calcdate)
) b ON (b.datecol = a.DateColumn)

答案 3 :(得分:0)

Another Approach:

with cte as(select 0 as n
union all
select n+1 from cte
where n<11)

select DateColumn,isnull(cnt,0) 'TotalCount' from (
select DATENAME(MONTH,dateadd(mm,n,'2000-01-01'))DateColumn from cte)x
left join 
(
select DATENAME(MONTH,InsertDateTime)DateColumn1 ,count (DATENAME(MONTH,InsertDateTime)) cnt from User t
where InsertDateTime>='2017-06-04 16:12:46.713' and InsertDateTime<=getdate()
group by DATENAME(MONTH,InsertDateTime)
  ) y on x.DateColumn=DateColumn1