MySQL复合体在表中联接

时间:2018-07-12 03:05:53

标签: mysql sql join

我有一个包含以下条目的MySQL会话表

if(comboBox.SelectedItem.ToString() == "90")

我希望sessionid conversationid agentid Reason s1 c1 a1 r1 s2 c1 a1 r2 s3 c2 a2 r1 s4 c3 a3 r2 用于sesionidagentid相同且首次出现原因为conversationid的行。

预期输出:

r1

1 个答案:

答案 0 :(得分:0)

select
  b.sessionid,
  b.conversationid,
  b.agentid,
  b.closeReason,
  b.assignTs
from
  (select
     *
   from
     session
   where
     substr(assignTs,1,10) = '2018-07-11'
  ) as b
inner join
  (select
     conversationid,
     agentid,
     count(*) c,
     sessionid,
     closeReason
   from
     session
   where
     substr(assignTs,1,10) = '2018-07-11'
   and
     conversationid in
     (select
        conversationid
      from
        session
      where
        closeReason = 'PENDING_CUST_UPDATE'
      and
        substr(assignTs,1,10) = '2018-07-11')
      group by
        conversationid, agentid having c > 1
     ) as a 
   on
     a.agentid = b.agentid
   and
     a.conversationid =b.conversationid
   and
     a.closeReason = 'PENDING_CUST_UPDATE'
   order by
     b.agentid,
     b.conversationid