select
q."Operators" as "Операторы",
q."Incoming" as "Входящие разговор более 1 сек.",
q."Manual Outgoing" as "Ручной Исходящий набор",
q."Outgoing campaign" as "Исходящие через кампанию",
q."Lost" as "Потерянные",
q."Total" as "Всего входящих",
from (select
coalesce(s."B2IDUser", s."AIDUser",s."B3IDUser") as "Operators",
count(s."ID") filter (where s."SeanceType" = 1 and "DurationTalk">0 ) as "Incoming",
count(s."ID") filter (where s."SeanceType" = 2 ) as "Manual Outgoing",
count(s."ID") filter (where s."SeanceType" = 2 and ("ANumber" like '%z%' or "B2IDUser" in (
5667834207, 5511332770, 5667834203, 5022050645, 5022050649,
5022050661, 5022050691, 5026073792, 5030972556, 5041139142,
5154248810, 5022050687, 5022050657, 5030698089, 6051167175,
5578934994, 5667834199, 6071889612, 5698847426, 5022050675,
5026918771, 5027841073, 5148236948, 5022050669, 5613031592
) ) ) as "Outgoing campaign",
count(s."ID") filter (where s."SeanceResult" = 191) as "Lost",
count(s.*) filter (where s."SeanceType" =1) as "Total",
count(s."ID") filter (where s."SeanceResult" in (192, 101, 102, 191, 151)) as "Percent"
from "S_Seances" s
where
(
s."B2IDUser" in (
5667834207, 5511332770, 5667834203, 5022050645, 5022050649,
5022050661, 5022050691, 5026073792, 5030972556, 5041139142,
5154248810, 5022050687, 5022050657, 5030698089, 6051167175,
5578934994, 5667834199, 6071889612, 5698847426, 5022050675,
5026918771, 5027841073, 5148236948, 5022050669, 5613031592
) or
s."AIDUser" in (
5667834207, 5511332770, 5667834203, 5022050645, 5022050649,
5022050661, 5022050691, 5026073792, 5030972556, 5041139142,
5154248810, 5022050687, 5022050657, 5030698089, 6051167175,
5578934994, 5667834199, 6071889612, 5698847426, 5022050675,
5026918771, 5027841073, 5148236948, 5022050669, 5613031592
) or
s."B3IDUser" in (
5667834207, 5511332770, 5667834203, 5022050645, 5022050649,
5022050661, 5022050691, 5026073792, 5030972556, 5041139142,
5154248810, 5022050687, 5022050657, 5030698089, 6051167175,
5578934994, 5667834199, 6071889612, 5698847426, 5022050675,
5026918771, 5027841073, 5148236948, 5022050669, 5613031592
)
) and
s."TimeStart" >= current_date
group by 1 ) as
q
group by q."Operators",q."Lost",q."Incoming",q."Percent",q."Total" ,q."Manual Outgoing",q."Outgoing campaign"