需要合并这个查询

时间:2018-09-28 07:07:04

标签: mysql

需要合并以下查询:-

    [HttpGet(@"interval")]
    [RequestSizeLimit(100000000)]
    public IActionResult Interval()
    {
        try
        {
            TimeSpan interval = new TimeSpan(0, 2, 5);
            Thread.Sleep(interval);
            return Ok("Hello world");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

有公共座席ID。

agent_activities具有agent_id,它是座席ID。 配置已创建为代理ID的

现在的结果是:-

select users.name as 'Agent', 
min(case when agent_activities.activity='login' then agent_activities.created_at end) as 'Login Time' , 
max(case when agent_activities.activity='logout' then agent_activities.created_at end) as 'Logout Time' 
from agent_activities inner join users 
on agent_activities.agent_id = users.id 
and date(agent_activities.created_at) = curdate() 
group by users.name 
order by users.name asc;

select created_name as 'Agent', count(*) as sale_count from dispositions where date(created_at) = curdate() and 
main_disp = 'Contactable' and sub_disp = 'Paid & Closed - Online' group by created_name order by sale_count desc;

select created_name as 'Agent', count(*) as dispositions_count from dispositions where date(created_at) = curdate() group by created_name 
order by dispositions_count desc;

所需结果:-

Agent   Login Time          Logout Time
Agent   2018-09-27 11:43:44 2018-09-27 11:45:04
Hari    2018-09-27 11:10:41 2018-09-27 15:18:04
test    2018-09-27 10:26:54 2018-09-27 11:43:41
ttt     NULL                2018-09-27 11:21:06

Agent   sale_count
Hari VH 1

Agent   dispositions_count
ttt     2
Hari    2
Agent   1

1 个答案:

答案 0 :(得分:1)

如基于agent_id的加入条件的注释中所述,我试图将三个查询合并为一个。

请检查是否可以提供所需的输出。

select u.name as 'Agent', 
min(CASE WHEN aa.activity='login' THEN aa.created_at ELSE null end) as 'Login Time' , 
max(CASE WHEN aa.activity='logout' THEN aa.created_at ELSE null end) as 'Logout Time' ,
SUM(CASE WHEN d.main_disp='Contactable' AND d.sub_disp = 'Paid & Closed - Online' THEN 1 ELSE 0 END) as sale_count,
count(*) as dispositions_count
from agent_activities aa
inner join users u on aa.agent_id = u.id and date(aa.created_at) = curdate() 
inner join dispositions d on d.created_by = aa.agent_id and date(d.created_at) = curdate()
group by u.name 
order by u.name asc;

您还可以在代理名称上使用LEFT OUTER JOIN来对所有三个查询进行核对。

select a.Agent as 'Agent', Login_Time, Logout_Time, sale_count, dispositions_count
FROM (
        select users.name as 'Agent', 
        min(case when agent_activities.activity='login' then agent_activities.created_at end) as Login_Time , 
        max(case when agent_activities.activity='logout' then agent_activities.created_at end) as Logout_Time
        from agent_activities inner join users 
        on agent_activities.agent_id = users.id 
        and date(agent_activities.created_at) = curdate() 
        group by users.name ) a
inner join (
    select created_name as 'Agent', count(*) as sale_count from dispositions where date(created_at) = curdate() and 
    main_disp = 'Contactable' and sub_disp = 'Paid & Closed - Online' group by created_name
) b on a.Agent = b.Agent
left outer join (
    select created_name as 'Agent', count(*) as dispositions_count from dispositions where date(created_at) = curdate() group by created_name 
) c on c.Agent = a.Agent
order by a.Agent asc;