在参数中使用 LIKE 来区分团队
Using LIKE in a parameter to distinguish between teams
我有一份报告,我想对其进行参数化,以便用户可以在查看专门团队、普通用户或两者的统计数据之间进行选择。如果我在 SQL 中手动区分,我会使用
Where [User Name] like ('%Dedicated')
专职团队和
Where [User Name] NOT like ('%Dedicated')
对于普通用户(敬业的团队成员始终在其用户名末尾标记 Dedicated)
我不确定将那些 SQL where 子句转换为 SSRS 参数表达式的语法。我试过了
= Like ('%Dedicated')
一个参数和
=Not Like ('%Dedicated')
另一个
= Like ('%')
两者都
和
Where [User name] @Team
在我的 SQL
但它抛出了一个错误 "Expression expected" 所以我一定是语法错误。谁能建议?我通常会将 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
编辑:不同的方法
尝试使用
而不是下面的 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 中设置您的参数以允许空值、文本类型且不接受多个值。使用此数据集为参数提供 values/labels。
希望这个跑得快一点!
在你的 SQL 中你可以做类似的事情..
iif(UserName like '%Dedicated', 1, 0) as [Dedicated]
(为简单起见,我使用 shorthand iif()
而不是 case
)
然后你可以过滤到..
where Dedicated in (@Parameter)
您的参数可以有多个定义值 -
- 专用 = 1
- 非专用 = 0
允许用户 select 多个值。
您可能必须将 SQL 放入 CTE 或派生的 table 才能使用您创建的 [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
我有一份报告,我想对其进行参数化,以便用户可以在查看专门团队、普通用户或两者的统计数据之间进行选择。如果我在 SQL 中手动区分,我会使用
Where [User Name] like ('%Dedicated')
专职团队和
Where [User Name] NOT like ('%Dedicated')
对于普通用户(敬业的团队成员始终在其用户名末尾标记 Dedicated)
我不确定将那些 SQL where 子句转换为 SSRS 参数表达式的语法。我试过了
= Like ('%Dedicated')
一个参数和
=Not Like ('%Dedicated')
另一个
= Like ('%')
两者都
和
Where [User name] @Team
在我的 SQL
但它抛出了一个错误 "Expression expected" 所以我一定是语法错误。谁能建议?我通常会将 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
编辑:不同的方法
尝试使用
而不是下面的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 中设置您的参数以允许空值、文本类型且不接受多个值。使用此数据集为参数提供 values/labels。
希望这个跑得快一点!
在你的 SQL 中你可以做类似的事情..
iif(UserName like '%Dedicated', 1, 0) as [Dedicated]
(为简单起见,我使用 shorthand iif()
而不是 case
)
然后你可以过滤到..
where Dedicated in (@Parameter)
您的参数可以有多个定义值 -
- 专用 = 1
- 非专用 = 0
允许用户 select 多个值。
您可能必须将 SQL 放入 CTE 或派生的 table 才能使用您创建的 [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