Selecting from another table without a join
我在下面有一个有效的查询,但是我需要在我的select语句中为这些代理选择名字和姓氏,而没有任何实际值可以连接表。
我的另一个表是ambition.ambition_users,它具有first_name,last_name和扩展名。我有子查询正确提取扩展并将其视为
在不影响当前查询的聚合的情况下实现拉名字和姓氏的最佳方法是什么?
这是工作查询:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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; |
n
我没有在您的表之间建立关系,但是您可以使用以下逻辑在select语句中获取带有子查询的名字和姓氏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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; |
代替再次使用相同的子查询