Oracle SQL: How return several distinct columns

血红的双手。 提交于 2021-01-29 04:51:04

问题


I have an Oracle Query that outputs

but I wish to output all distinct column except for Incident ID, i.e.

I tried adding GROUP BY to Oracle Query but get error,

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

Below is my Query ... please assist

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

回答1:


Try this

 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



回答2:


Fix the JOIN syntax and use aggregation:

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

All unaggregated columns need to be in the GROUP BY.

Why do the source and date have to match for the join to INCIDENT as well as the message id? It would seem that the message id should be sufficient.



来源:https://stackoverflow.com/questions/56009847/oracle-sql-how-return-several-distinct-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!