具有自定义日期范围的SQL查询

时间:2012-10-14 01:46:43

标签: sql

我有一个自定义SQL查询,它返回“上个月”的信息我现在需要将日期范围从上个月更改为....上个月的19日到当月的20日。

这就是显示“上个月”的内容任何人都可以帮助修改它以显示所需的日期范围。

SELECT  TOP 10000 CONVERT(DateTime,
LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)),
101) AS SummaryMonth,
Nodes.Caption AS NodeName,
Interfaces.Caption AS Interface_Caption,
SUM(InterfaceTraffic.In_TotalBytes) AS SUM_of_Total_Bytes_Received,
SUM(InterfaceTraffic.Out_TotalBytes) AS SUM_of_Total_Bytes_Transmitted,
SUM((NullIf(In_TotalBytes,-2)+NullIf(Out_TotalBytes,-2))) AS SUM_of_TotalBytesRecvXmit,
Interfaces.Gig_Limit AS Gig_Limit,
SUM((NullIf(In_TotalBytes,-2)+NullIf(Out_TotalBytes,-2))) - (Interfaces.Gig_Limit *    1073741824)  AS Diff

FROM 
(Nodes INNER JOIN Interfaces ON (Nodes.NodeID = Interfaces.NodeID))  INNER JOIN        InterfaceTraffic ON (Interfaces.InterfaceID = InterfaceTraffic.InterfaceID AND InterfaceTraffic.NodeID = Nodes.NodeID)


WHERE 
( DateTime >= dateadd(mm,datediff(mm,0,getdate())-1,0) AND DateTime
<dateadd(mm,datediff(mm,0,getdate()),0) )
 AND  
(
  (Interfaces.Caption LIKE '%SM%') OR 
  (Interfaces.Caption LIKE '%County Snow%')
)


GROUP BY CONVERT(DateTime, LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)),   101), 
Nodes.Caption, Interfaces.Caption, Interfaces.Gig_Limit

Having SUM((NullIf(In_TotalBytes,-2)+NullIf(Out_TotalBytes,-2))) - (Interfaces.Gig_Limit * 1073741824) > 0


ORDER BY SummaryMonth ASC, 6 DESC

由于 戴夫

1 个答案:

答案 0 :(得分:1)

WHERE (DateTime >= 18+dateadd(mm,datediff(mm,0,getdate())-1,0)
  AND  DateTime <  19+dateadd(mm,datediff(mm,0,getdate()),0))

我从字面上看你,即19th of LAST to 20th of CURRENT,虽然从20世纪末到19世纪可能更有意义。虽然很容易调整数字。