SSRS语法 - Countdistinct([列A],其中[列B] = X)

时间:2017-06-14 13:18:53

标签: reporting-services

我有一个如下图所示的数据集。我需要在SSRS中使用它做很多事情,但我遇到的问题是某些引用(电话)有多个结果但应该只计算一次。我不确定如何将条件置于一个独特的计数中。

我需要的是

  • 不同参考号的计数,其类型为“普通操作员呼叫”(附图中的22)
  • 不同参考编号的平均连接持续时间(261.14)
  • 开始时间介于08:30和17:00之间的不同参考数类型为“普通操作员呼叫”(19)

我知道您可以在SSRS中使用countdistinct但我不确定计数的正确语法 >条件是什么。在此先感谢您的任何帮助。

enter image description here

编辑:按请求添加SQL查询

B.id

1 个答案:

答案 0 :(得分:0)

在我看来,这在SSRS中很棘手。我会尝试在SQL中执行此操作,并且不知道哪些表可以在下面找到哪些列。 Calc1,Calc2和Calc3列将始终相同,因此您可以将它们插入到页脚行中,并将MAX作为聚合。

create table #final 
(
    reference varchar(50),
    [Call Time] datetime,
    [type] nvarchar(500),
    [Ring Time] int,
    [connected] int,
    [Outcome] nvarchar(500),
    Calc1 int,
    Calc2 int,
    Calc3 int
)

--put current query into temp table:
insert into #final
(
    reference ,
    [Call Time] ,
    [type] ,
    [Ring Time] ,
    [connected] ,
    [Outcome]
)
SELECT
    [reference]
    ,dateadd(hh,1,[Start Time]) AS [Call Time]
    ,[type]
    ,[ring (ms)]/1000 AS [Ring Time]
    ,[connected (ms)]/1000 AS [connected]
    ,rtrim([rhoutcome]) AS [Outcome] 
From [InboundCallsView] AS ICV  
LEFT JOIN ianalyse.[dbo].[iAnalyse3_iResultsHeaderXML] IRES 
ON  ICV.[Activation ID]  = IRES.[rhActivationId] 
    AND ires.[rhAccountNo] = '310169'
    AND ([rhstarttime] BETWEEN '2017-06-13' AND '2017-06-14' OR [rhStartTime]    IS null)
WHERE [account] = '310169'
AND dateadd(hh,1,[Start Time]) BETWEEN '2017-06-13' AND '2017-06-14'
AND [type] <> 'Call diverted'

-----Get the calculations:
--calc1
update f
set Calc1 = b.Calc1
from #final f
inner join 
    (select
            count(Distinct reference) as Calc1
     from #final f
     where [type] = 'normal operator call'
     ) as b
     on 1=1

--calc2
update f
set Calc2 = b.Calc2
from #final f
inner join 
    (

    select --get the average per distinct reference
        avg(connectedDistinct) as Calc2
    from 
            (--distinct reference by max Connected
            select
                 reference,
                 max([connected]) as connectedDistinct
             from #final f 
             group by 
                reference
             ) as b2
    ) as b
     on 1=1

--calc3
update f
set Calc3 = b.Calc3
from #final f
inner join 
    (  
            select
                count(Distinct reference) as Calc3
            from #final f 
            where
              convert(time, [Call Time]) >= '08:30:00.0000000'
               and convert(time, [Call Time]) <= '17:00:00.0000000'
    ) as b
     on 1=1


select
    reference ,
    [Call Time] ,
    [type] ,
    [Ring Time] ,
    [connected] ,
    [Outcome] ,
    Calc1 ,
    Calc2 ,
    Calc3 
from #final
order by
    [Call Time]