简体   繁体   English

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

[英]SSRS Report Query - BETWEEN within WHERE clause

I have an SSRS report that I am creating that shows various counters collected on some SQL servers over the last several months. 我正在创建一个SSRS报告,该报告显示了过去几个月在某些SQL服务器上收集的各种计数器。 I have the report working, however I want to add an enhancement, but can't quite figure out how to do it. 我的报告正在运行,但是我想添加一个增强功能,但是还不太清楚该怎么做。

Basically, I want to query my "Performance Stats" table, and only select records that are between 2 dates specified in the report. 基本上,我想查询我的“性能统计信息”表,并且只选择报表中指定的2个日期之间的记录。 I also want to further reduce that amount by hours of the day, based on a parameter. 我还希望根据参数在一天中的小时内进一步减少该金额。 For example, I would like to be able to select all records from 01/01/2014 thru 01/02/2014 and it will return all those records into a graph I have defined. 例如,我希望能够选择2014年1月1日至2014年2月1日的所有记录,它将所有这些记录返回到我定义的图形中。

Furthermore, I would then like to have a dropdown box, where the report user can pick the hours of when the CPU counter was recorded, such as "Business Hours Only" (8:00am - 5:00pm) or "Outside Business Hours Only" (5:00pm - 08:00am) or even both. 此外,我想创建一个下拉框,报表用户可以在其中选择记录CPU计数器的时间,例如“仅营业时间”(8:00 am-5:00 pm)或“仅营业时间以外” ”(下午5:00-上午08:00),甚至两者都有。

EDIT: 编辑:

OK - I've got the script working with the below: OK-我的脚本适用于以下情况:

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

However when SSRS executes it, the function returns an error: 但是,当SSRS执行它时,该函数返回错误:

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

This is how SSRS is executing it (from profiler): 这是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'

you can try & take the reference from below script.. 您可以尝试从以下脚本中获取参考。

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

Try to declare var @hours as int with 3 states possible DECLARE @Hours INT 尝试将var @hours声明为int,并可能具有3种状态DECLARE @Hours INT

@Hours: @小时:

BH - 1 BH-1

OBH - 2 OBH-2

BH or OBH - 3--both BH或OBH-三项

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

I was able to get everything working without the split function by just doing this: 通过执行以下操作,我能够在不使用split功能的情况下使所有工作正常:

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

Thanks for all your help guys :) 谢谢您的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM