在我的oracle选择查询中获取重复项

时间:2012-04-26 05:14:04

标签: sql oracle oracle10g

以下是我对结果的Oracle选择查询,但我得到了我不需要的重复项。我认为很难找到其他人,请试一试。

SELECT I.EID                         EID,
       I.WT                          Title,
       I.RID                         RID,
       I.FORMNAME                    STAGENAME,
       I.FORMS                       STATUS,
       I.INPT                        Projects,
       To_char(I.DVDD, 'DD/MM/YYYY') DVDD,
       CASE
         WHEN B.SID = 2
              AND B.DID = 2 THEN Trunc(F.EVENTDATE) - I.DVDD
         ELSE NULL
       END                           DEVLAPSEDAYS,
       To_char(I.MDD, 'DD/MM/YYYY')  MDD,
       CASE
         WHEN B.SID = 2
              AND B.DID = 2 THEN Trunc(F.EVENTDATE) - I.MDD
         ELSE NULL
       END                           MONITORLAPSEDAYS,
       L.LDEID                       LEADEID
FROM   table1 I,
       table2 F,
       table3 B,
       table4 L
WHERE  I.ACTIVEFLG = 1
       AND I.LATESTFLG = 1
       AND I.FORMSTATUS IN ( 1, 3 )
       AND I.UNIQUEID = F.UNIQUEID
       AND B.SID = 2
       AND B.DID IN ( 2, 3 )
       AND ( F.EVENTDATE > I.DVDD
              OR F.EVENTDATE > I.MDD )
       AND ( ( Trunc(F.EVENTDATE) - I.DVDD ) > 2
              OR ( Trunc(F.EVENTDATE) - I.MDD ) > 2 )
       AND F.LINKID = B.LINKID
       AND I.FORMSTAGENAME IN ( 'Develop', 'Monitor' ) 

2 个答案:

答案 0 :(得分:2)

您没有将TABLE4(别名为L)链接到列表中的任何其他表格,从而创建了一个笛卡尔积。 这可能会生成您的重复记录。
加入TABLE4到其他表之一,你应该好好去。

请参阅:http://docs.oracle.com/cd/B14117_01/server.101/b10759/queries006.htm

中的笛卡尔积

这也是使用SQL-92语法而不是旧SQL-96的一个很好的理由。它强制您明确指定表连接。
我已经使用SQL-92重写了您的查询,您只需要为TABLE4插入连接条件,它应该适合您。

希望它有所帮助...

SELECT I.EID                         EID,
       I.WT                          Title,
       I.RID                         RID,
       I.FORMNAME                    STAGENAME,
       I.FORMS                       STATUS,
       I.INPT                        Projects,
       To_char(I.DVDD, 'DD/MM/YYYY') DVDD,
       (CASE
           WHEN B.SID = 2
            AND B.DID = 2 
           THEN Trunc(F.EVENTDATE) - I.DVDD
           ELSE NULL
        END)                         DEVLAPSEDAYS,
       To_char(I.MDD, 'DD/MM/YYYY')  MDD,
       (CASE
         WHEN B.SID = 2
          AND B.DID = 2 
         THEN Trunc(F.EVENTDATE) - I.MDD
         ELSE NULL
        END)                         MONITORLAPSEDAYS,
       L.LDEID                       LEADEID
  FROM table1 I
 INNER JOIN table2 F ON (I.UNIQUEID = F.UNIQUEID)
 INNER JOIN table3 B ON (F.LINKID = B.LINKID)
 INNER JOIN table4 L ON (<insert join clause here>)
 WHERE I.ACTIVEFLG = 1
   AND I.LATESTFLG = 1
   AND I.FORMSTATUS IN ( 1, 3 )
   AND B.SID = 2
   AND B.DID IN ( 2, 3 )
   AND ( F.EVENTDATE > I.DVDD OR F.EVENTDATE > I.MDD )
   AND ( ( Trunc(F.EVENTDATE) - I.DVDD ) > 2 OR ( Trunc(F.EVENTDATE) - I.MDD ) > 2 )
   AND I.FORMSTAGENAME IN ( 'Develop', 'Monitor' ) 

答案 1 :(得分:0)

我对此类信息的最佳做法是告诉您将整个查询分组:

   GROUP BY I.EID,I.WT,I.RID,I.FORMNAME,I.FORMS,I.INPT,
   To_char(I.DVDD, 'DD/MM/YYYY') DVDD,
   CASE
     WHEN B.SID = 2
          AND B.DID = 2 THEN Trunc(F.EVENTDATE) - I.DVDD
     ELSE NULL
   END,
   To_char(I.MDD, 'DD/MM/YYYY')  MDD,
   CASE
     WHEN B.SID = 2
          AND B.DID = 2 THEN Trunc(F.EVENTDATE) - I.MDD
     ELSE NULL
   END,
   L.LDEID