SSRS:SQL IF布尔表达式1或布尔表达式2不工作

时间:2017-04-13 04:02:35

标签: sql-server reporting-services

我正在研究SSRS项目,我有4个参数 @ DateFrom,@ DateTo,@ VhGroup,@代理。案例是@Agent OR @VHGroup等于'(空白)'然后else语句将执行。但我似乎无法使其发挥作用。这是sql语句

IF @VHGroup <> '(Blank)' OR @Agent <> '(Blank)'
    SELECT [Call ID of Abandoned]                                                                                               as CallIdAbandoned
    ,[InboundQueue]                                                                                                             as AbandonedCallQueue
    ,CONVERT(DATETIME2(0),[OriginalDateTime])                                                                                                           as CallArivalDateTime
    ,[OriginalEndDateTime]                                                                                                      as AbandonedDateTime
    ,[OriginalDuration]                                                                                                         as AbandonedDuration
    ,[CallIDVH]                                                                                                                                             as AgentGroup
    ,CASE WHEN [Outcome] = 'Handled Note : Call Could not be connected' THEN 'Subs Unavailable' ELSE [OutCome] END              as Outcome
    ,[TalkTime]                                                                                                                 as OutBoundDuration
     FROM [AgentStatisticsEOD].[dbo].[VH_Data]
     WHERE CONVERT(Date,[SCBCallDateTimeRequest]) BETWEEN @DateFrom and @DateTo 
     AND [AgentGroup]  collate database_default in (SELECT value from dbo.FnSplit(@VHGroup,','))
     AND [CallOutAgentName] collate database_default in (SELECT value from dbo.FnSplit(@Agent,','))
ELSE
    SELECT [Call ID of Abandoned]                                                                                               as CallIdAbandoned
    ,[InboundQueue]                                                                                                             as AbandonedCallQueue
    ,CONVERT(DATETIME2(0),[OriginalDateTime])                                                                                                           as CallArivalDateTime
    ,[OriginalEndDateTime]                                                                                                      as AbandonedDateTime
    ,[OriginalDuration]                                                                                                         as AbandonedDuration
    ,[CallIDVH]                                                                                                                                                 as AgentGroup
    ,CASE WHEN [Outcome] = 'Handled Note : Call Could not be connected' THEN 'Subs Unavailable' ELSE [OutCome] END              as Outcome
    ,[TalkTime]                                                                                                                 as OutBoundDuration
     FROM [AgentStatisticsEOD].[dbo].[VH_Data]
     WHERE CONVERT(Date,[SCBCallDateTimeRequest]) BETWEEN @DateFrom and @DateTo
     AND [AgentGroup]  collate database_default in (SELECT value from dbo.FnSplit(@VHGroup,','))
     OR [CallOutAgentName] collate database_default in (SELECT value from dbo.FnSplit(@Agent,','))

2 个答案:

答案 0 :(得分:0)

你的逻辑是错误的,IF语句应该像这样使用AND

IF @VHGroup <> '(Blank)' AND @Agent <> '(Blank)'
    --snip
ELSE
    --snip

答案 1 :(得分:0)

我不确定你的确切逻辑,但这种方法需要更少的重复代码:

在此查询中,如果任一参数为空,则根本不会对其进行过滤。

绝对确定参数值是真的(Blank)(不是''NULL

    SELECT [Call ID of Abandoned]                                                                                               as CallIdAbandoned
    ,[InboundQueue]                                                                                                             as AbandonedCallQueue
    ,CONVERT(DATETIME2(0),[OriginalDateTime])                                                                                                           as CallArivalDateTime
    ,[OriginalEndDateTime]                                                                                                      as AbandonedDateTime
    ,[OriginalDuration]                                                                                                         as AbandonedDuration
    ,[CallIDVH]                                                                                                                                             as AgentGroup
    ,CASE WHEN [Outcome] = 'Handled Note : Call Could not be connected' THEN 'Subs Unavailable' ELSE [OutCome] END              as Outcome
    ,[TalkTime]                                                                                                                 as OutBoundDuration
     FROM [AgentStatisticsEOD].[dbo].[VH_Data]
     WHERE CONVERT(Date,[SCBCallDateTimeRequest]) BETWEEN @DateFrom and @DateTo 
     AND 
     (
        @VHGroup = '(Blank)' 
        OR 
        [AgentGroup]  collate database_default in (
            SELECT value from dbo.FnSplit(@VHGroup,',')
        )
     )
     AND
     (
        @Agent = '(Blank)'
        OR
        [CallOutAgentName] collate database_default in (
           SELECT value from dbo.FnSplit(@Agent,',')
        )
     )