这是我的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
答案 0 :(得分: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))
答案 1 :(得分: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