Oracle SQL INNER JOIN и Count (*) = 1 не избавляются от дубликатов - PullRequest
0 голосов
/ 30 мая 2019

Мой оригинальный Oracle SQL Query (в конце поста) выводит

enter image description here

но я хочу вывести это

enter image description here

Мне посоветовали использовать INNER JOIN для удаления любых дубликатов.

Это не сработало.

Тогда мне посоветовали использовать HAVING COUNT(*)=1

Это частично работает

Вместо 3 или 4 дубликатов, максимум 2 дубликата.

Есть идеи?

Оригинальный SQL-запрос

SELECT TO_CHAR(MIN(I.INCIDENTID))                  AS "Incident ID",
          MIN(I.CREATIONDATE)                      AS "Creation Date",
          TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY')     AS "Date",
          TRIM(MO.DOMAINUSERNAME)                  AS "Login ID",  
          TRIM(M.MESSAGESUBJECT)                   AS "Email Subject"
   FROM  INCIDENT I 
   JOIN  MESSAGE M
   ON M.MESSAGEID = I.MESSAGEID
   JOIN   MESSAGEORIGINATOR MO
   ON     M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID           
   GROUP BY TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY'),
            TRIM(MO.DOMAINUSERNAME),
            TRIM(M.MESSAGESUBJECT)

Идентичный запрос, кроме того, что я добавил INNER JOIN и HAVING COUNT(*)=1

SELECT TO_CHAR(MIN(I.INCIDENTID))                  AS "Incident ID",
          MIN(I.CREATIONDATE)                      AS "Creation Date",
          TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY')     AS "Date",
          TRIM(MO.DOMAINUSERNAME)                  AS "Login ID",  
          TRIM(M.MESSAGESUBJECT)                   AS "Email Subject"
   FROM  INCIDENT I 
   INNER JOIN  MESSAGE M
   ON M.MESSAGEID = I.MESSAGEID
   INNER JOIN   MESSAGEORIGINATOR MO
   ON     M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID           
   GROUP BY TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY'),
            TRIM(MO.DOMAINUSERNAME),
            TRIM(M.MESSAGESUBJECT)
HAVING COUNT(*)=1

1 Ответ

1 голос
/ 30 мая 2019

использовать row_number ()

  with cte as (  SELECT TO_CHAR(I.INCIDENTID))                  AS "Incident ID",
              (I.CREATIONDATE)                      AS "Creation Date",
              TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY')     AS "Date",
              TRIM(MO.DOMAINUSERNAME)                  AS "Login ID",  
              TRIM(M.MESSAGESUBJECT)                   AS "Email Subject",
              row_number() over(partition by TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY'),
            TRIM(MO.DOMAINUSERNAME), TRIM(M.MESSAGESUBJECT) order by TO_CHAR(I.INCIDENTID)),I.CREATIONDATE) rn 
       FROM  INCIDENT I 
       JOIN  MESSAGE M
       ON M.MESSAGEID = I.MESSAGEID
       JOIN   MESSAGEORIGINATOR MO
       ON     M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID 
) select * from cte where rn=1
...