Oracle SQL查询 - 加入Max()?

时间:2017-09-21 11:08:09

标签: sql oracle join nested

所以,我想要实现的目的是看看客户上周是否向我们发送了一封电子邮件 - 如果是 - 请查看客户在此之前的7天内通过电子邮件向我们发送过多少次。

示例:

  

hithere@stackoverflow.com上周(星期四)发送了一封电子邮件。因此,子查询应该从上周的星期四-7天查找该电子邮件并计算多少。

请参阅下面的查询,如果我完全忽略了E join它工作正常,但我似乎无法在MAX()上加入。

有没有解决办法?

SELECT 
D.LABEL,
B.EMAILADDRESS,
MAX(TRUNC(A.COMPLETEDATE)) AS LastCompleted,
COUNT(E.MESSAGEID) AS count

FROM KC_MESSAGE A

LEFT OUTER JOIN KC_EMAILADDRESS B ON A.SENDERID = B.EMAILADDRESSID
LEFT OUTER JOIN KC_FOLDER C ON A.QUEUEID = C.FOLDERID
LEFT OUTER JOIN KC_DEPARTMENT D ON C.DEPARTMENTID = D.DEPARTMENTID

LEFT OUTER JOIN KC_MESSAGE E ON E.SENDERID = A.SENDERID 
AND TRUNC(E.COMPLETEDAY) BETWEEN MAX(TRUNC(A.COMPLETEDATE))-7 AND MAX(TRUNC(A.COMPLETEDATE))

WHERE TRUNC(A.COMPLETEDATE) BETWEEN TRUNC(SYSDATE-10) AND TRUNC(SYSDATE-4)
AND A.COMPLETEEVENTID = 9 

GROUP BY D.LABEL, B.EMAILADDRESS

0 个答案:

没有答案