<?xml version="1.0" encoding="utf-16"?>
<ReportFile Version="100901">
  <Report>
    <Id>21a3fdb7-fdf3-44c3-939e-fdb1db65b83d</Id>
    <Name>_Входящие звонки по номерам (интервальный)</Name>
    <GroupName>_</GroupName>
    <Type>1</Type>
    <ViewType>3</ViewType>
    <ViewTag>0</ViewTag>
    <Description>_Показывает количество входящих звонков по внешним номерам. Каждый принятый или потерянный звонок считается за одну единицу. 
Доступны распределения по часам, по дням, по неделям и по месяцам. 
Для расчета введите интервал дат и выберите тип распределения. </Description>
    <TimerSec>0</TimerSec>
    <Details IsNull="0" Compiled="1">
      <sqltext>_-----------По часам-----------------------
if @inter=1
begin

with cte (number, datecnt, cnt)
as
(

select *
from
(
select top 1000 a.anumber as [Номер],
	   a.datecnt as [Дата],
	   sum(a.cnt) as [Количество]
from
(

select case 
			when coalesce(anumberdialed,'')='' then ''
			else anumberdialed 
			end as anumber,
	  convert(varchar(10),sc.timestart,104)+' '+right('00'+cast(datepart(hh,sc.timestart) as nvarchar(2)),2)+':00-'+right('00'+cast(datepart(hh,sc.timestart)+1 as nvarchar(2)),2)+':00' as datecnt, 
      count(distinct sc.idchain) as cnt
--      sc.timestart as timestart
from oktell.dbo.a_stat_connections_1x1 sc
	where sc.connectiontype in (4) and
		  sc.timestart between @dt1 and @dt2
	group by sc.anumberdialed,convert(varchar(10),sc.timestart,104)+' '+right('00'+cast(datepart(hh,sc.timestart) as nvarchar(2)),2)+':00-'+right('00'+cast(datepart(hh,sc.timestart)+1 as nvarchar(2)),2)+':00'
)a
group by a.anumber,a.datecnt
order by a.datecnt
) b



union all

select  ' Всего по входящим' as [Номер],
		'Итого:' as [Дата],
		count(distinct sc.idchain) as [Количество]
from oktell.dbo.a_stat_connections_1x1 sc
	where sc.connectiontype in (4) and
		  sc.timestart between @dt1 and @dt2
)

	  
select ' Всего по входящим' as [Номер],datecnt as [Дата],sum(cnt) as [Количество]
from cte		  
group by datecnt
union
select number as [Номер],
	   datecnt as [Дата],
	   cnt as [Количество]
from cte
order by [Номер] desc

end

-----------По дням-----------------------
if @inter=2
begin
with cte (number, datecnt, cnt)
as
(


		select 
			   b.[Номер],
			   b.[Дата],
			   sum(b.[Количество]) as [Количество]
		from
		(
			select case 
					when coalesce(anumberdialed,'')='' then ''
					else anumberdialed 
					end as [Номер],
			   CONVERT(varchar(10), sc.timestart , 104) as [Дата],
			   count(distinct sc.idchain) as [Количество] 
			from oktell.dbo.a_stat_connections_1x1 sc
			where sc.connectiontype in (4) and
				  sc.timestart between @dt1 and @dt2
			group by sc.anumberdialed,CONVERT(varchar(10), sc.timestart , 104)
		 ) b
		 group by b.[Дата],b.[Номер]
		 
		 union

		select  ' Всего по входящим' as [Номер],
				/*convert(varchar(10),@dt1,104)+'-'+convert(varchar(10),@dt2,104)*/ 'Итого:' as [Дата],
				count(distinct sc.idchain) as [Количество]

		from oktell.dbo.a_stat_connections_1x1 sc
			where sc.connectiontype in (4) and
				  sc.timestart between @dt1 and @dt2
	
)

	  
	select  number as [Номер],
		   datecnt as [Дата],
	  	   cnt as [Количество]
	from cte

	union

	select ' Всего по входящим' as [Номер],datecnt as [Дата],sum(cnt) as [Количество]
	from cte		  
	group by datecnt

    order by [Номер] desc



end


-----------По неделям-----------------------
if @inter=3
begin
with cte (number, datecnt, cnt)
as
(


		select *
		from
		(
		select case 
					when coalesce(a.anumber,'')='' then ''
					else a.anumber 
					end as [Номер],
			   a.date1 as [Дата],
			   sum(a.cnt) as [Количество]
		from
		(	select anumberdialed as anumber,
			   right('000'+cast(datepart(wk,sc.timestart) as nvarchar(2)),2)+'-я неделя' as date1,
			   count(distinct sc.idchain) as cnt  
			from oktell.dbo.a_stat_connections_1x1 sc
			where sc.connectiontype in (4) and
				  sc.timestart between @dt1 and @dt2
			group by anumberdialed,right('000'+cast(datepart(wk,sc.timestart) as nvarchar(2)),2)+'-я неделя'
		 )a
		 group by a.anumber,a.date1
		 ) b
		 
		 union all

		select  ' Всего по входящим' as [Номер],
				/*convert(varchar(10),@dt1,104)+'-'+convert(varchar(10),@dt2,104)*/'Итого:' as [Дата],
				count(distinct sc.idchain) as [Количество]
		from oktell.dbo.a_stat_connections_1x1 sc
			where sc.connectiontype in (4) and
				  sc.timestart between @dt1 and @dt2
				  
)

	  
select ' Всего по входящим' as [Номер],datecnt as [Дата],sum(cnt) as [Количество]
from cte		  
group by datecnt
union
select number as [Номер],
	   datecnt as [Дата],
	   cnt as [Количество]
from cte
order by [Номер] desc


end

-----------По месяцам-----------------------
if @inter=4
begin

with cte (number, datecnt, cnt)
as
(


		select top 10000 *
		from
		(
		select case 
					when coalesce(a.anumber,'')='' then ''
					else a.anumber 
					end as [Номер],
			   a.date1 as [Дата],
			   sum(a.cnt) as [Количество]
		from
		(	select anumberdialed as anumber,
			   right('000'+cast(datepart(mm,sc.timestart) as nvarchar(2)),2)+' месяц' as date1,
			   count(distinct sc.idchain) as cnt  
			from oktell.dbo.a_stat_connections_1x1 sc
			where sc.connectiontype in (4) and
				  sc.timestart between @dt1 and @dt2
			group by anumberdialed,right('000'+cast(datepart(mm,sc.timestart) as nvarchar(2)),2)+' месяц'
		 )a
		 group by a.anumber,a.date1
		 ) b
		 
		 union all

		select top 10000  ' Всего по входящим' as [Номер],
				/*convert(varchar(10),@dt1,104)+'-'+convert(varchar(10),@dt2,104)*/'Итого:' as [Дата],
				count(distinct sc.idchain) as [Количество]
		from oktell.dbo.a_stat_connections_1x1 sc
			where sc.connectiontype in (4) and
				  sc.timestart between @dt1 and @dt2
       order by [Дата]
				  
)

	  
select ' Всего по входящим' as [Номер],datecnt as [Дата],sum(cnt) as [Количество]
from cte		  
group by datecnt
union
select number as [Номер],
	   datecnt as [Дата],
	   cnt as [Количество]
from cte
order by [Номер] desc

end
</sqltext>
      <GroupFields1 Count="1">
        <gf100>_Дата</gf100>
        <gd100>_Дата</gd100>
      </GroupFields1>
      <GroupFields2 Count="1">
        <gf200>_Номер</gf200>
        <gd200>_Номер</gd200>
      </GroupFields2>
      <ValueFields Count="1">
        <vf00>_Количество</vf00>
        <vd00>_Количество</vd00>
        <vs00>_</vs00>
      </ValueFields>
      <Filter IsNull="0" Count="3">
        <fi00 IsNull="0">
          <Id>1</Id>
          <Caption>_Дата</Caption>
          <Code>_@dt1</Code>
          <FilterType>2</FilterType>
          <Query>_</Query>
        </fi00>
        <fi01 IsNull="0">
          <Id>2</Id>
          <Caption>_</Caption>
          <Code>_@dt2</Code>
          <FilterType>3</FilterType>
          <Query>_</Query>
        </fi01>
        <fi02 IsNull="0">
          <Id>3</Id>
          <Caption>_Распределение</Caption>
          <Code>_@inter</Code>
          <FilterType>51</FilterType>
          <Query>_create table #t (interv int,name nvarchar(50))
insert into #t values (1,'По часам')
insert into #t values (2,'По дням')
insert into #t values (3,'По неделям')
insert into #t values (4,'По месяцам')

select interv,name from #t
</Query>
        </fi02>
      </Filter>
    </Details>
  </Report>
</ReportFile>