0

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

例如,我希望 4 月 = 到 4 月 + 1 月、Fev、3 月……等等,所以 6 月 = 到 6 月 + 1 月、2 月、3 月、4 月、5 月……

SELECT COUNT(*) As 'Deployed',  
    DATENAME(YEAR, LastModified) As 'Year', 
    DATEPART(Month, LastModified), 
    DATENAME(Month, LastModified) As 'Month'
FROM [InventoryDatabase].[dbo].[Hardware_RefreshList] 
WHERE Asset_Type = 'Laptop' AND Status = 'Completed' AND Department != 'SNBc' 
      AND DATENAME(YEAR, LastModified) = '2017' 
GROUP BY DATEPART(Month, LastModified), DATENAME(YEAR, LastModified), 
         Month(LastModified), DATENAME(Month, LastModified), 
         DATEADD(MONTH, DATEDIFF(MONTH, 0, LastModified), 0)
ORDER BY  DATEPART(Month, LastModified)
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

4

1 回答 1

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)
于 2017-01-24T16:18:54.357 回答