简体   繁体   中英

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. 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. 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.

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.

EDIT:

OK - I've got the script working with the below:

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:

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

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

@Hours:

BH - 1

OBH - 2

BH or OBH - 3--both

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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