Microsoft SQL每月添加到查询

时间:2017-01-24 15:18:31

标签: sql-server calculation

所以我在.net中为图表构建查询。我的问题是,我不知道如何将月份加到下个月。所以这是我在下面的查询以及我从中获得的内容。

我想这样,例如,4月将是= 4月+ 1月,Fev,3月......等等6月将是= 6月+ 1月,2月,3月,4月,5月...... / p>

Result from the query
2    2017    3   March
  6   2017    4   April
  8   2017    6   June
  6   2017    7   July
  9   2017    9   September
  29  2017    10  October
  10  2017    11  November
  54  2017    12  December
  
     

{{1}}

1 个答案:

答案 0 :(得分:0)

您可以使用count() over()

执行此操作
select 
     [Year]      = datepart(year , LastModified)
   , [Month]     = datepart(Month, LastModified) 
   , [MonthName] = datename(Month, LastModified) 
   , DeployedThisMonth = count(*)
   , DeployedSoFar = count(*) over (
        order by datepart(year , LastModified)
               , datepart(Month, LastModified)
     )
  from [InventoryDatabase].[dbo].[Hardware_RefreshList]
  where Asset_Type = 'Laptop' 
    and status = 'Completed' 
    and Department != 'snbc' 
    and datepart(year, LastModified) = 2017
  group by 
      datepart(Month, LastModified)
    , datepart(year, LastModified)
    , Month(LastModified)
    , datename(Month, LastModified)
    --, dateadd(month, datediff(month, 0, LastModified), 0)
  order by datepart(Month, LastModified)