确定时间间隔

时间:2016-03-03 10:24:11

标签: sql sql-server reporting-services ssrs-2008-r2

我尝试在SSRS中创建一个图表,该图表只显示星期日晚上10点到晚上11点的价值。

我的查询:

select intervaldateweek as Week,
       SUM(GoodUnits) As GoodUnits,
       SUM(NetUnits) As NetUnits,
       SUM(GoodUnits)  / NULLIF(SUM(NetUnits) , 0.0)* 100 As Value
from Count
    inner join tsystem ON Count.systemid = tsystem.ID
where IntervalDate >= getdate()-300
  and tsystem.ID = 2 
group by intervaldate

我用这个表达式尝试只获得星期日:

(DATEPART(dw, IntervalDate) = 1

但我不知道如何确定具体的时间间隔。在这种情况下,晚上10点至晚上11点。

我需要这个时间段之间的值

在22:00:00到23:00:00之间

 IntervalDate >= DATEADD(HOUR, 1,CAST(DATEADD(DAY,0, CAST(GETDATE() AS DATE)) AS DATETIME))
    AND IntervalDate <= DATEADD(HOUR, 22, CAST(CAST(GETDATE() AS DATE) AS DATETIME))

我用这个表达式尝试了它,但我想我在表达式中设置了错误的麻木。

3 个答案:

答案 0 :(得分:0)

你可以使用以下行..

DATEPART(dw,GETDATE()) = 1 AND CAST(GETDATE() AS TIME(0)) BETWEEN CAST('22:00:00' AS TIME(0)) AND CAST('23:00:00' AS TIME(0))

不需要使用那种东西。

答案 1 :(得分:0)

试试这个

 select intervaldateweek as Week,
   SUM(GoodUnits) As GoodUnits,
   SUM(NetUnits) As NetUnits,
   SUM(GoodUnits)  / NULLIF(SUM(NetUnits) , 0.0)* 100 As Value
from Count
inner join tsystem ON Count.systemid = tsystem.ID
where DATEPART(dw,intervaldate) = 1 AND CAST(intervaldate AS TIME(0))
BETWEEN CAST('22:00:00' AS TIME(0)) AND CAST('23:00:00' AS TIME(0))
and tsystem.ID = 2
group by intervaldate

答案 2 :(得分:0)

您可以使用DATEPART功能(documentation here)查看相关部分,例如日期时间值的小时。例如:

SELECT 
    intervaldateweek AS [Week],
    SUM(GoodUnits) AS GoodUnits,
    SUM(NetUnits) AS NetUnits,
    SUM(GoodUnits) / NULLIF(SUM(NetUnits), 0.0)* 100 AS Value
FROM 
    [Count]
    INNER JOIN tsystem ON [Count].systemid = tsystem.ID
WHERE 
    DATEPART(WEEKDAY,intervaldate) = 1 /* See note about SET DATEFIRST */
    AND DATEPART(HOUR, intervaldate) = 22
    AND tsystem.ID = 2
GROUP BY
    intervaldateweek

注意:使用DATEPART(WEEKDAY, ...)功能时,请务必了解您的服务器配置,因为有时候星期日会是1,星期日的其他时间会是7.您可以使用例如SET DATEFIRST 1来强制它它可以预测。有关详细信息,请参阅此MSDN link

PS:您的选择语句按intervaldate分组,但您的列列表使用的intervaldateweek无效。我将假设该分组的目的是intervaldateweek