Oracle SQL:如何返回几个不同的列

时间:2019-05-06 17:29:48

标签: sql oracle group-by

我有一个输出的Oracle查询

enter image description here

但是我希望输出除事件ID(即

)以外的所有不同的列

enter image description here

我尝试将GROUP BY添加到Oracle Query,但出现错误,

DataSource.Error: Oracle: ORA-00979: not a GROUP BY expression
Details:
    DataSourceKind=Oracle
    DataSourcePath=dlporacle
    Message=ORA-00979: not a GROUP BY expression
    ErrorCode=-2147467259

以下是我的查询...请协助

SELECT 
   I.INCIDENTID                    AS "Incident ID",
   I.CREATIONDATE                  AS "Creation Date",
   MO.IPADDRESS           AS "IP Address",
   MO.DOMAINUSERNAME               AS "Login ID",
   MO.ENDPOINTMACHINENAME          AS "Computer Name",
   M.MESSAGESUBJECT                AS "Email Subject"
 FROM MESSAGE M,
 JOIN INCIDENT I ON M.MESSAGEID = I.MESSAGEID AND M.MESSAGESOURCE = I.MESSAGESOURCE AND  M.MESSAGEDATE  = I.MESSAGEDATE
 JOIN MESSAGEORIGINATOR MO ON  M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID
 WHERE  M.MESSAGESOURCE = 'ENDPOINT'
    AND I.ISDELETED = 0
  GROUP BY MO.IPADDRESS, MO.DOMAINUSERNAME, MO.ENDPOINTMACHINENAME, M.MESSAGESUBJECT

2 个答案:

答案 0 :(得分:1)

尝试一下

 SELECT MIN(I.INCIDENTID)               AS "Incident ID",
        I.CREATIONDATE                  AS "Creation Date",
        MO.IPADDRESS                    AS "IP Address",
        MO.DOMAINUSERNAME               AS "Login ID",
        MO.ENDPOINTMACHINENAME          AS "Computer Name",
        M.MESSAGESUBJECT                AS "Email Subject"
   FROM MESSAGE M,
        INCIDENT I,
        MESSAGEORIGINATOR MO
  WHERE M.MESSAGESOURCE = 'ENDPOINT'
    AND M.MESSAGESOURCE = I.MESSAGESOURCE
    AND M.MESSAGEID = I.MESSAGEID
    AND M.MESSAGEDATE = I.MESSAGEDATE 
    AND M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID 
    AND I.ISDELETED = 0
  GROUP BY I.CREATIONDATE, MO.IPADDRESS, MO.DOMAINUSERNAME, MO.ENDPOINTMACHINENAME,M.MESSAGESUBJECT

答案 1 :(得分:1)

修复JOIN语法并使用聚合:

SELECT
   MIN(I.INCIDENTID)      AS "Incident ID",
   I.CREATIONDATE                  AS "Creation Date",
   MO.IPADDRESS           AS "IP Address",
   MO.DOMAINUSERNAME               AS "Login ID",
   MO.ENDPOINTMACHINENAME          AS "Computer Name",
   M.MESSAGESUBJECT                AS "Email Subject"
FROM MESSAGE M JOIN
     INCIDENT I
     ON M.MESSAGEID = I.MESSAGEID AND
        M.MESSAGESOURCE = I.MESSAGESOURCE AND
        M.MESSAGEDATE = I.MESSAGEDATE JOIN
     MESSAGEORIGINATOR MO
     ON  M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID
WHERE M.MESSAGESOURCE = 'ENDPOINT' AND I.ISDELETED = 0
GROUP BY I.CREATIONDATE, MO.IPADDRESS, MO.DOMAINUSERNAME, MO.ENDPOINTMACHINENAME, M.MESSAGESUBJECT

所有未聚合的列必须位于GROUP BY中。

为什么加入INCIDENT的消息的来源和日期以及消息ID必须匹配?看来消息ID应该足够。