Sql 返回所有月份,而不是仅返回4个月

Sql 返回所有月份,而不是仅返回4个月,sql,sql-server,Sql,Sql Server,我从一个数据库中得到了一些数据,但这只是今年以来的4个月。我希望它能在12个月内全部归还。我正在使用SQLServerManagementStudio 2010。下面是sql代码 SELECT Month = datename(month,dateadd(mm,datediff(mm,0,StartTime),0)), Count (TestName) FROM VExecutionGlobalHistory Where Tester <> 'dit2988'

我从一个数据库中得到了一些数据,但这只是今年以来的4个月。我希望它能在12个月内全部归还。我正在使用SQLServerManagementStudio 2010。下面是sql代码

SELECT  Month   = datename(month,dateadd(mm,datediff(mm,0,StartTime),0)), Count   (TestName) 
FROM VExecutionGlobalHistory
 Where Tester <> 'dit2988'       
group by
dateadd(mm,datediff(mm,0,StartTime),0), year(dateadd(mm,datediff(mm,0,StartTime),0))
order by
dateadd(mm,datediff(mm,0,StartTime),0), year(dateadd(mm,datediff(mm,0,StartTime),0))
我还想在报税表中添加一些硬编码数据,并将7月作为最近的一个月,因此我会有类似的内容

|August   |number|
|September|number|
|October  |number|
|November |number|   
|December |number|
|January  |number|
|February |number|
|March    |number|
|April    |92    |
|May      |79    |
|June     |164   |
|July     |162   |
所有的数字都不是从数据库中提取出来的,只是我想输入的


建议?

只需创建一个从初始日期到您想要的最终日期的计数,并按月数和年份与您的数据进行左连接:

DECLARE @start_date DATETIME, @end_date DATETIME;

SELECT @start_date = CAST('2010-08-01' AS DATETIME),
  @end_date = CAST('2011-07-01' AS DATETIME);

WITH MonthTally(initial_date,month_name, month_number, year_number)  AS
(
  SELECT @start_date initial_date, datename(month,@start_date) month_name, month(@start_date) month_number, year(@start_date) year_number
  UNION ALL
  SELECT DATEADD(month,1,initial_date), datename(month, DATEADD(month,1,initial_date)), month(DATEADD(month,1,initial_date)), year(DATEADD(month,1,initial_date))
  FROM MonthTally
  WHERE initial_date < @end_date
)
SELECT month_name, Count(TestName) 
FROM MonthTally MT
    LEFT JOIN VExecutionGlobalHistory V ON MT.year_number=YEAR(V.StartTime) AND MT.month_number=MONTH(V.StartTime) AND Tester <> 'dit2988'       
group by
  month_name, year_number, month_number
order by
  year_number, month_number

检查功能示例。

这不会返回任何数据,我是否遗漏了什么?您是否尝试将@start\u date和@end\u date的值更改为要搜索的时间范围?如果是这样,那么,请尝试从Monthtaly选择*以检查CTE生成的月份,而不是选择。您还可以发布一个存储在VExecutionGlobalHistory上的数据示例,我将根据您的示例以SQLFiddle发布查询。我检查了代码,删除了group by和order by子句中一些不必要的字段。请小心使用@start_date和@end_date值。年和月是重要的值。日期以ODBC规范格式编写。如果CAST不起作用,您可以尝试改为CONVERTdatetime,“2010-08-01 00:00:00.000”,1202行VExecutionGlobalHisotry ID示例| Tester | Environment | StartTime | EndTime | TestName | 4 | dit2988 | ito5 | 2013-07-20 | 2013-07-20 | Hold Order | 5 | dit2989 | ito4 | 2013-06-20 | Account您的查询没有返回其他月份的数据,因为在Vexecutiongpet月份的历史记录中没有数据可能用于测试仪“dit2988”。
DECLARE @start_date DATETIME, @end_date DATETIME;

SELECT @start_date = CAST('2010-08-01' AS DATETIME),
  @end_date = CAST('2011-07-01' AS DATETIME);

WITH MonthTally(initial_date,month_name, month_number, year_number)  AS
(
  SELECT @start_date initial_date, datename(month,@start_date) month_name, month(@start_date) month_number, year(@start_date) year_number
  UNION ALL
  SELECT DATEADD(month,1,initial_date), datename(month, DATEADD(month,1,initial_date)), month(DATEADD(month,1,initial_date)), year(DATEADD(month,1,initial_date))
  FROM MonthTally
  WHERE initial_date < @end_date
)
SELECT month_name, Count(TestName) 
FROM MonthTally MT
    LEFT JOIN VExecutionGlobalHistory V ON MT.year_number=YEAR(V.StartTime) AND MT.month_number=MONTH(V.StartTime) AND Tester <> 'dit2988'       
group by
  month_name, year_number, month_number
order by
  year_number, month_number