SSRS报告查询 - WHERE子句中的BETWEEN

时间:2014-11-12 08:24:57

标签: sql-server reporting-services parameters

我正在创建一个SSRS报告,显示过去几个月在某些SQL服务器上收集的各种计数器。我有报告工作,但我想添加一个增强功能,但无法弄清楚如何做到这一点。

基本上,我想查询“Performance Stats”表,并且只选择报告中指定的2个日期之间的记录。我还希望根据参数进一步减少一天中的小时数。例如,我希望能够从01/01/2014到2014年2月1日选择所有记录,它会将所有这些记录返回到我定义的图表中。

此外,我希望有一个下拉框,报告用户可以选择记录CPU计数器的小时数,例如“仅限营业时间”(上午8:00 - 下午5:00)或“外部”营业时间“(下午5:00 - 上午08:00),甚至两者兼而有之。

修改

好的 - 我已经让脚本使用了以下内容:

DECLARE @DateFrom DATETIME = DATEADD(month, -1, GETDATE())
DECLARE @DateThru DATETIME = GETDATE()
DECLARE @Hours INT = 1

SELECT  Stat_Datetime ,
        cntr_value
FROM    Admin.dbo.PerformanceCounters
WHERE   object_name = 'Processor'
        AND Stat_Datetime BETWEEN @DateFrom AND @DateThru
        AND ( 1 IN (SELECT * FROM dbo.fnSplitString(@Hours, ''))
              AND ( (CAST(Stat_Datetime AS TIME) BETWEEN '08:00'
                                                 AND     '17:00') )
              OR ( 2 IN (SELECT * FROM dbo.fnSplitString(@Hours, ''))
                   AND ( ( CAST(Stat_Datetime AS TIME) >= '17:00' )
                         OR ( CAST(Stat_Datetime AS TIME) < '08:00' )
                       )
                   OR ( 1 IN (SELECT * FROM dbo.fnSplitString(@Hours, '')) AND 2 IN (SELECT * FROM dbo.fnSplitString(@Hours, '')))
                 )
            )
ORDER BY stat_datetime

但是当SSRS执行它时,该函数会返回错误:

Msg 8144, Level 16, State 3, Line 6
Procedure or function dbo.fnSplitString has too many arguments specified.

这就是SSRS执行它的方式(来自探查器):

exec sp_executesql N'SELECT  Stat_Datetime ,
        cntr_value
FROM    Admin.dbo.PerformanceCounters
WHERE   object_name = ''Processor''
        AND Stat_Datetime BETWEEN @DateFrom AND @DateThru
        AND ( 1 IN (SELECT * FROM dbo.fnSplitString(1,2, ''''))
              AND ( (CAST(Stat_Datetime AS TIME) BETWEEN ''08:00''
                                                 AND     ''17:00'') )
              OR ( 2 IN (SELECT * FROM dbo.fnSplitString(1,2, ''''))
                   AND ( ( CAST(Stat_Datetime AS TIME) >= ''17:00'' )
                         OR ( CAST(Stat_Datetime AS TIME) < ''08:00'' )
                       )
                   OR ( 1 IN (SELECT * FROM dbo.fnSplitString(1,2, '''')) AND 2 IN (SELECT * FROM dbo.fnSplitString(1,2, '''')))
                 )
            )
ORDER BY stat_datetime',N'@DateFrom datetime,@DateThru datetime',@DateFrom='2014-08-13 00:00:00',@DateThru='2014-11-13 00:00:00'

3 个答案:

答案 0 :(得分:0)

你可以试试&amp;从以下脚本中获取参考..

SELECT *
FROM
(    
SELECT   Stat_Datetime ,
        cntr_value,
        CASE 
        WHEN Stat_Datetime BETWEEN CAST(CONVERT(VARCHAR(10), Stat_Datetime , 110) + ' 08:00:00' AS DATETIME)
                     AND CAST(CONVERT(VARCHAR(10), Stat_Datetime , 110) + ' 17:00:00' AS DATETIME) THEN 'BH'
        WHEN Stat_Datetime BETWEEN CAST(CONVERT(VARCHAR(10), Stat_Datetime , 110) + ' 17:00:00' AS DATETIME)
                     AND CAST(CONVERT(VARCHAR(10), Stat_Datetime, 110) + ' 08:00:00' AS DATETIME) THEN 'OBH'                     
        END AS 'BHours'
FROM    Admin.dbo.PerformanceCounters
WHERE   object_name = 'Processor'
)Result
WHere BHours IN('BH','OBH')

答案 1 :(得分:0)

尝试将var @hours声明为int,可能有3种状态 DECLARE @Hours INT

@Hours:

BH - 1

OBH - 2

BH或OBH - 3 - 两者

AND (@Hours = 1 
    AND (
        (CAST(Stat_Datetime AS TIME) BETWEEN '08:00' AND '17:00')
        )
OR (@Hours = 2
    AND (
        (CAST(Stat_Datetime AS TIME) BETWEEN '17:00' AND '08:00')
        )
    )
OR (@Hours = 3)--both

答案 2 :(得分:0)

通过这样做,我能够在没有拆分功能的情况下完成所有工作:

DECLARE @DateFrom DATETIME = DATEADD(month, -1, GETDATE())
DECLARE @DateThru DATETIME = GETDATE()
DECLARE @Hours INT = 1

SELECT  Stat_Datetime ,
        cntr_value
FROM    Admin.dbo.PerformanceCounters
WHERE   object_name = 'Processor'
        AND Stat_Datetime BETWEEN @DateFrom AND @DateThru
        AND ( 1 IN ( @Hours )
              AND ( (CAST(Stat_Datetime AS TIME) BETWEEN '08:00'
                                                 AND     '17:00') )
              OR ( 2 IN ( @Hours )
                   AND ( ( CAST(Stat_Datetime AS TIME) >= '17:00' )
                         OR ( CAST(Stat_Datetime AS TIME) < '08:00' )
                       )
                 )
            )
ORDER BY stat_datetime

感谢所有帮助人员:)