-2

以下是我对结果的 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' ) 
4

2 回答 2

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' ) 
于 2012-04-27T14:00:53.110 回答
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
于 2012-04-27T14:00:03.863 回答