Oracle
 sql >> Base de données >  >> RDS >> Oracle

Groupe ORACLE SQL en donnant TOUJOURS des doublons

Si CREATIONDATE est un type de données de date, il contient l'élément de temps jusqu'aux secondes. Il est très peu probable que l'identifiant d'incident que vous recherchez le plus bas ait l'heure exacte. Formatez la date en une date ISO et GROUP BY ignorera les décalages horaires.

SELECT TO_CHAR(MIN(I.INCIDENTID))      AS "Incident ID",
          to_char(I.CREATIONDATE, 'yyyy-mm-dd') AS "Creation Date",
          M.MESSAGESUBJECT                AS "Email Subject",
          MO.IPADDRESS                    AS "IP Address",
          MO.DOMAINUSERNAME               AS "Login ID",
          MO.ENDPOINTMACHINENAME          AS "Computer Name"
   FROM        MESSAGE M 
   LEFT JOIN   INCIDENT I 
   ON          M.MESSAGESOURCE = I.MESSAGESOURCE 
   AND         M.MESSAGEID = I.MESSAGEID
   AND         M.MESSAGEDATE    = I.MESSAGEDATE 
   LEFT JOIN   MESSAGEORIGINATOR MO
   ON          M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID
   LEFT JOIN   MESSAGEEXT ME
   ON          ME.MESSAGEID = M.MESSAGEID
   LEFT JOIN   INCIDENTSTATUS S
   ON          S.INCIDENTSTATUSID = I.INCIDENTSTATUSID
   LEFT JOIN   CUSTOMATTRIBUTESRECORD C
   ON          C.CUSTOMATTRIBUTESRECORDID = I.CUSTOMATTRIBUTESRECORDID                    
   GROUP BY    to_char(I.CREATIONDATE, 'yyyy-mm-dd') 
               M.MESSAGESUBJECT,
               MO.IPADDRESS,
               MO.DOMAINUSERNAME,
               MO.ENDPOINTMACHINENAME