我在下面有一个工作查询,但我需要在我的select语句中为这些代理选择名字和姓氏,没有任何实际值加入表格。
我的另一个表是ambition.ambition_users,它有first_name,last_name和extension。我有子查询正确拉动扩展并将其视为id
。但是,在我的主要选择中,我还想选择first_name和last_name。我似乎无法通过一个简单的子查询找到一种方法,除非我能够在ambition_users上加入我创建的id
,否则我无法加入该表。 .extention。
在不影响当前查询聚合的情况下,实现提取名字和姓氏的最佳方法是什么?
这是工作查询:
select
case
when callingpartyno in (select extension from ambition.ambition_users)
then callingpartyno
when finallycalledpartyno in (select extension from ambition.ambition_users)
then finallycalledpartyno
end as id
-- this is where i want to select first_name and last_name from ambition.ambition_users
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0)) as total_calls
, now() as time_of_report
, curdate() as date_of_report
from
ambition.session a
join ambition.callsummary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate()
and (
callingpartyno in (select extension from ambition.ambition_users
)
or finallycalledpartyno in (select extension from ambition.ambition_users
)
)
group by
id;
答案 0 :(得分:1)
我没有在你的表之间建立关系,但你可以使用下面的逻辑来在select语句中使用子查询获取名字和/或姓名
select
case
when callingpartyno in (select extension from ambition.ambition_users)
then callingpartyno
when finallycalledpartyno in (select extension from ambition.ambition_users)
then finallycalledpartyno
end as id
-- this is where i want to select first_name and last_name from ambition.ambition_users
Max((select firstname from ambition.ambition_users as t1 where t1. Extension=b.callingpartyno)),
Max((select lastname from ambition.ambition_users as t1 where t1. Extension=b.callingpartyno)),
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0)) as total_calls
, now() as time_of_report
, curdate() as date_of_report
from
ambition.session a
join ambition.callsummary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate()
and (
callingpartyno in (select extension from ambition.ambition_users
)
or finallycalledpartyno in (select extension from ambition.ambition_users
)
)
group by
id;
答案 1 :(得分:1)
为什么需要这么多子查询。您可以使用左连接并取出所有其他子查询。
select
COALESCE( callingpartyno, finallycalledpartyno) as id
-- if extension is not equal to callingpartyno, it will return null because its a left join. If you use coalesce you can get the first non null value.
, max(c.firstname)
, max(c.lastname)
-- if you use max you need not put that in group by clause
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0)) as total_calls
, now() as time_of_report
, curdate() as date_of_report
from
ambition.session a
join ambition.callsummary b
on a.notablecallid = b.notablecallid
---- adding a left join
left join
ambition.users c
on c.extension = callingpartyno or c.extension = finallycalledpartyno
where
date(a.ts) >= curdate()
group by id;
答案 2 :(得分:0)
而不是再次使用相同的子查询&再次,我建议你将子查询的结果存储在一个变量中。
此外,您可以针对callingpartyno
和finallycalledpartyno
撰写不同的查询,然后将结果与UNION
合并,如下所示:
select @ext := Group_concat(distinct extension separator ',') from ambition.ambition_users;
select tmp.*,
(select firstn from ambition.ambition_users where tmp.id = extension) as First_Name,
(select lastn from ambition.ambition_users where tmp.id = extension) as Last_Name
From
(select
callingpartyno as id
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0)) as total_calls
, now() as time_of_report
, curdate() as date_of_report
from
ambition.session a
join ambition.callsummary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate()
and
callingpartyno in (@ext)
group by callingpartyno)
UNION
(select
finallycalledpartyno as id
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0)) as total_calls
, now() as time_of_report
, curdate() as date_of_report
from
ambition.session a
join ambition.callsummary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate()
and
finallycalledpartyno in (@ext)
group by finallycalledpartyno)
) tmp;