在参数中使用LIKE来区分团队

时间:2017-05-31 11:23:37

标签: sql reporting-services ssrs-2012

我有一份报告,我想参与其中,以便用户可以在查看专用团队,普通用户或两者的统计数据之间进行选择。如果我在SQL中手动区分,我会使用

  

[用户名]如何('%Dedicated')

专门的团队和

  

[用户名]不喜欢('%Dedicated')

对于普通用户(专门的团队成员总是将Dedicated标记到用户名的末尾)

我不确定将那些SQL where子句转换为SSRS参数表达式的语法。我试过了

  

=喜欢('%Dedicated')

表示一个参数和

  

=不喜欢('%Dedicated')

为另一个

  

=喜欢('%')

两者

  

[用户名] @Team

在我的SQL

但它引发了一个错误"表达预期"所以我必须弄错语法。任何人都可以建议吗?我通常会将LIKE直接放入SQL中,但在这种情况下,它有时需要像是,有时不喜欢取决于所选的选项。

编辑添加SQL:

 WITH [data] as

(
SELECT 

iif([Agent Name] like '%Robert Dyas', 1, 0) as [Dedicated]

,convert(date,format(Dateadd(hh,1,[Start Time]),'dd/MM/yyyy'),103) as [Date], 
[Client Name],
[Account],
sum(iif([Type] in ('Normal operator call','Caller rang off','Caller rang off during divert','No suitable operator logged on'),1,0)) as [Calls Offered],
sum(iif([Type] in ('Normal operator call'),1,0)) as [Calls Answered],
sum(iif([Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'),1,0)) as [Ring Offs],
sum(iif([Ring (secs)] <= 20 AND [Type] in ('Normal operator call'),1,0)) as [Answered in 20 Secs],
sum(iif([Type] in ('Normal operator call'),[Ring (secs)],NULL)) as [Total Ring Time],
sum([connected (secs)]) as [Total Connected Time],
sum(iif([Ring (secs)] > 5 AND [Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'),1,0)) as [CRO After 5 Secs],
avg(iif([Type] in ('Normal operator call'),[Ring (secs)],null)) as 'Avg Time to Answer',

avg(iif([Type] in ('Normal operator call'),[Connected (secs)],null)) as 'Avg Call Time'


 ,sum(iif(rtrim([rhoutcome]) = 'MESSAGE',1,0)) AS [Total Messages]

  FROM ipr.dbo.InboundCallsView IC left JOIN [iAnalyse].[dbo].[iAnalyse3_iResultsHeaderXML] ires ON ic.[Reference]   = ires.[rhcallref]


  WHERE [account] = '106844'


  AND Dateadd(hh,1,[Start Time]) between '2017-05-29' AND '2017-05-31'





  group BY iif([Agent Name] like '%Robert Dyas', 1, 0), format(Dateadd(hh,1,[Start Time]),'dd/MM/yyyy'),[client name],[account]

)

select 
    *
from [data] as [d]
where d.Dedicated = 1

1 个答案:

答案 0 :(得分:1)

编辑:不同方法

而不是下面的iif(),请尝试使用

where ([Agent Name] like '%' + (@Parameter) or (@Parameter) is null)

您可以使用小数据集为参数创建值:

select 
    null as [Value],
    'All' as [Label]

union all

select 
    'Dedicated' as [Value],
    'Dedicated' as [Label]

union all

select
    '' as [Value],
    'Not Dedicated' as [Label]

在SSRS中设置参数以允许空值,类型为Text,并且不接受多个值。使用此数据集为参数提供值/标签。

希望这个运行得快一点!

在你的SQL中你可以做类似的事情。

iif(UserName like '%Dedicated', 1, 0) as [Dedicated]

(为简单起见,我使用简写iif()而不是case

然后你可以过滤到..

where Dedicated in (@Parameter)

您的参数可以有多个定义的值 -

  • Dedicated = 1
  • Not Dedicated = 0

允许用户选择多个值。

您可能必须将SQL放入CTE或派生表中才能使用您创建的[Dedicated]字段。

这是我在CTE中如何做到这一点的模拟:

with [data] as
(
    select 
        *,
        iif(t.UserName like '%Dedicated', 1, 0) as [Dedicated]
    from dbo.[Table] as [t]
)
select 
    *
from [data] as [d]
where d.Dedicated in (@Parameter)

我将如何处理您的SQL的版本:

with [data] as
(
    select 
        iif([Agent Name] like '%Robert Dyas', 1, 0) as [Dedicated],
        convert(date, format(Dateadd(hour, 1, [Start Time]),'dd/MM/yyyy'),103) as [Date], 
        [Client Name],
        [Account],
        iif([Type] in ('Normal operator call', 'Caller rang off', 'Caller rang off during divert', 'No suitable operator logged on'), 1, 0) as [Calls Offered],
        iif([Type] = 'Normal operator call', 1, 0) as [Calls Answered],
        iif([Type] in ('Caller rang off', 'Caller rang off during divert', 'No suitable operator logged on'), 1, 0) as [Ring Offs],
        iif([Ring (secs)] <= 20 and [Type] = 'Normal operator call', 1, 0) as [Answered in 20 Secs],
        iif([Type] = 'Normal operator call', [Ring (secs)], null) as [Total Ring Time],
        [connected (secs)] as [Total Connected Time],
        iif([Ring (secs)] > 5 and [Type] in ('Caller rang off', 'Caller rang off during divert', 'No suitable operator logged on'), 1, 0) as [CRO After 5 Secs],
        iif([Type] = 'Normal operator call', [Ring (secs)], null) as [Avg Time to Answer],
        iif([Type] = 'Normal operator call' ,[Connected (secs)], null) as [Avg Call Time],
        iif(rtrim([rhoutcome]) = 'MESSAGE',1,0) as [Total Messages]
    from ipr.dbo.InboundCallsView as [IC] 
        left join [iAnalyse].[dbo].[iAnalyse3_iResultsHeaderXML] as [ires] on ic.[Reference] = ires.[rhcallref]
    where [account] = '106844'
        and Dateadd(hour, 1, [Start Time]) between '2017-05-29' and '2017-05-31'
)
select 
    d.[Date],
    d.[Client Name],
    d.Account,
    sum(d.[Calls Offered]) as [Calls Offered],
    sum(d.[Calls Answered]) as [Calls Answered],
    sum(d.[Ring Offs]) as [Ring Offs],
    sum(d.[Answered in 20 Secs]) as [Answered in 20 Secs],
    sum(d.[Total Ring Time]) as [Total Ring Time],
    sum(d.[Total Connected Time]) as [Total Connected Time],
    sum(d.[CRO After 5 Secs]) as [CRO After 5 Secs],
    avg(d.[Avg Time to Answer]) as [Avg Time to Answer],
    avg(d.[Avg Call Time]) as [Avg Call Time],
    sum(d.[Total Messages]) as [Total Messages]
from [data] as [d]
where d.Dedicated = 1
group by 
    d.[Date],
    d.[Client Name],
    d.Account