选择分组查询

时间:2015-07-06 00:56:01

标签: sql

我有以下查询,它为每个'SENDER_REFERENCE_NB'返回两个项目,一个是MESSAGE_STATUS是'(接收)',另一个是MESSAGE_STATUS'匹配'。两者都包含CREATE_TM

select MESSAGE_ID, CREATE_TM, MESSAGE_STATUS
from TMS_MESSAGE_AUDIT_LOG
where message_id in (select message_id from tms_message
                     WHERE SENDER_REFERENCE_NB like '150703%')
  and MESSAGE_STATUS in ('(Receive)', 'Matched');

我想要实现的是每个SENDER_REFERENCE_NB,我看到('Receive')状态&与此条目相关联的CREATE_TM和“匹配”状态以及CREATE_TM关联,全部在一行上。

1 个答案:

答案 0 :(得分:0)

做出一些假设,这是我的答案:

Select L1.MESSAGE_ID, L1.CREATE_TM, L1.MESSAGE_STATUS, 
       L2.MESSAGE_ID, L2.CREATE_TM, L2.MESSAGE_STATUS
from TMS_MESSAGE_AUDIT_LOG AS L1
INNER JOIN TMS_MESSAGE_AUDIT_LOG AS L2
ON L1.MESSAGE_STATUS = '(Receive)'
AND L1.SENDER_REFERENCE_NB like '150703%'
AND L2.SENDER_REFERENCE_NB like '150703%'
AND L2.MESSAGE_STATUS = 'Matched'

您可以在操作in this fiddle中看到它。