1

这是我的sql代码:

SELECT  Month   = datename(month,dateadd(mm,datediff(mm,0,StartTime),0)), 
Cast ((ROUND(((CONVERT(DECIMAL(10,2),SUM(Duration)/60)/60)),2)*55) AS numeric(36,2)) 
FROM VExecutionGlobalHistory Where Tester <> 'dit2988' AND TestTypeID = 1 

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))

此查询返回:

April|123
  May|3456
 June|856
 July|345

我希望它返回以下内容:

  May|3456
 June|856
 July|345

有没有人有任何想法?我正在使用 Microsoft SQL Server Management Studio 2010

4

2 回答 2

1

以下将返回该月的第一天,即从今天起 2 个月后:

SELECT CAST(DATEADD(month, DATEDIFF(month, 0, GETDATE())-2, 0)AS DATE)

因此,您可以添加到您的WHERE标准:

SELECT  Month   = datename(month,dateadd(mm,datediff(mm,0,StartTime),0)), 
Cast ((ROUND(((CONVERT(DECIMAL(10,2),SUM(Duration)/60)/60)),2)*55) AS numeric(36,2)) 
FROM VExecutionGlobalHistory 
Where Tester <> 'dit2988' AND TestTypeID = 1 
      AND StartTime >= CAST(DATEADD(month, DATEDIFF(month, 0, GETDATE())-2, 0) AS DATE)
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))
于 2013-07-31T20:04:58.757 回答
0
SELECT TOP 12 b.Month, b.Amount
FROM
(
      SELECT TOP 12 a.Year, a.SortMonth, a.Month, a.Amount
  FROM
  (
        SELECT 
              Year = datename(Year,StartTime),
              SortMonth = month(StartTime),
              Month = datename(month,dateadd(mm,datediff(mm,0,StartTime),0)), 
              Amount = Cast ((ROUND(((CONVERT(DECIMAL(10,2),SUM(Duration)/60)/60)),2)*55) AS numeric(36,2))
        FROM VExecutionGlobalHistory 
        Where Tester <> 'dit2988' 
          AND TestTypeID = 1 

        group by
              datename(Year,StartTime),
              month(StartTime),
              dateadd(mm,datediff(mm,0,StartTime),0), 
              year(dateadd(mm,datediff(mm,0,StartTime),0))
  ) a
  ORDER BY a.Year, a.SortMonth DESC
) b
ORDER BY b.Year, b.SortMonth
于 2013-08-01T14:48:48.560 回答