SQL Query的工作方式与预期不同

时间:2012-10-18 11:28:10

标签: sql database plsql oracle10g

我在理解sql语句时遇到问题。 当我执行它时(见下文),我只得到sgtn_typ'L'的人。

 SELECT DISTINCT kun_id kid
                ,dep_id
                ,ddg_rang_g RANG
                ,dep_perf_g
                ,TO_CHAR(dep_buchwert + dep_kasse, '999G999G990D99') WERT
                ,kun_name1
                ,Kun_vorname
                ,kun_cust_id
                ,kun_strasse
                ,kun_plz
                ,kun_ort
                ,kun_e_mail
                ,kun_pseudonym
                ,kka_value
   FROM ddg, dep, kun, kat, kka, sgtn
  WHERE dep_spi_id = 245
        AND dep_abr_id = 24
        AND ddg_dgr_id = 3
        AND ddg_abr_id = dep_abr_id
        AND ddg_spi_id = dep_spi_id
        AND ddg_dep_id = dep_id
        AND dep_kun_id = kun_id
        AND kun_aktiv = 'J'
        AND dep_spi_id = kun_spi_id
        AND dep_id IS NOT NULL
        AND dep_id NOT IN (SELECT ddg_dep_id
                             FROM ddg
                            WHERE ddg_spi_id = 245
                                  AND ddg_abr_id = 0
                                  AND ddg_dgr_id = 1)
        AND kun_id = kka_kun_id
        AND kka_spi_id = 245
        AND kat_spi_id = 245
        AND kka_kat_kuerzel = kat_kuerzel
        AND kat_kuerzel = 'NSCH'
        AND kka_spi_id = sgtn_spi_id
        AND kka_kun_id = sgtn_kun_id
        AND sgtn_spi_id = 245
        AND sgtn_e_mail = kun_e_mail
        AND (sgtn_typ = 'L' 
           OR (sgtn_typ = 'T'
              AND sgtn_e_mail NOT IN (SELECT sgtn_e_mail
                                      FROM sgtn
                                      WHERE sgtn_spi_id = 245
                                      AND sgtn_typ = 'L'))
          )
  ORDER BY kun_id, sgtn_typ;

但是当我执行此操作时,我会让人们使用sgtn_typ'T':

 SELECT *
   FROM sgtn
  WHERE sgtn_spi_id = 245
        AND (sgtn_typ = 'L' 
             OR (sgtn_typ = 'T'
                 AND sgtn_e_mail NOT IN (SELECT sgtn_e_mail
                                         FROM sgtn
                                         WHERE sgtn_spi_id = 245
                                         AND sgtn_typ = 'L'))
 )
  ORDER BY sgtn_kun_id;

我不明白为什么?有人能给我任何线索吗?我期待的是,我替换了所有类型为'L'的人,并且会得到'T'类型的人:

 AND (sgtn_typ = 'L'
         OR (sgtn_typ = 'T' 
              AND sgtn_e_mail NOT IN (SELECT sgtn_e_mail
                                     FROM sgtn
                                     WHERE sgtn_spi_id = 245
                                      AND sgtn_typ = 'L'))
       )

sgtn表中有'重复'(有些人输入'T'和'L')。除此之外,整个记录都是一样的。

表格方案如下:

SGTN:

SGTN_ID             NUMBER(22)
SGTN_SPI_ID             NUMBER(22)
SGTN_KUN_ID             NUMBER(22)
SGTN_TYP                VARCHAR2(4)
SGTN_GESCHLECHT     VARCHAR2(4)
SGTN_VORNAME        VARCHAR2(160)
SGTN_NAME1              VARCHAR2(240)
SGTN_STRASSE        VARCHAR2(240)
SGTN_PLZ                VARCHAR2(60)
SGTN_ORT                VARCHAR2(240)
SGTN_E_MAIL             VARCHAR2(240)
SGTN_GEB_DAT            DATE(7)
SGTN_KLASSE             VARCHAR2(200)
SGTN_SCHULE             VARCHAR2(200)
SGTN_TELEFON        VARCHAR2(120)
SGTN_KONTONUMMER        VARCHAR2(120)

KUN:

KUN_ID          NUMBER(22)
KUN_SPI_ID          NUMBER(22)
KUN_LAN_ID          NUMBER(22)
KUN_MED_KBEZ    VARCHAR2(40)
KUN_NAME1           VARCHAR2(240)
KUN_MKZ         VARCHAR2(80)
KUN_TS          DATE(7)
KUN_AKTIV           VARCHAR2(12)
KUN_KTOINH          VARCHAR2(240)
KUN_BLZ         VARCHAR2(40)
KUN_BANK            VARCHAR2(240)
KUN_KTONR           VARCHAR2(80)
KUN_ZAHLART         VARCHAR2(4)
KUN_NAME2           VARCHAR2(240)
KUN_VORNAME         VARCHAR2(160)
KUN_STRASSE         VARCHAR2(240)
KUN_PLZ         VARCHAR2(60)
KUN_ORT         VARCHAR2(240)
KUN_TEL1            VARCHAR2(120)
KUN_TEL2            VARCHAR2(120)
KUN_FAX         VARCHAR2(120)
KUN_E_MAIL          VARCHAR2(240)
KUN_PSEUDONYM   VARCHAR2(120)
KUN_PWD         VARCHAR2(60)
KUN_CUST_ID         VARCHAR2(240)
KUN_GESCHLECHT  VARCHAR2(4)
KUN_K_NAME          VARCHAR2(80)
KUN_K_NR            NUMBER(22)
KUN_K_VALID         VARCHAR2(40)
KUN_ZUSATZ_1    VARCHAR2(400)
KUN_ZUSATZ_DATUM    DATE(7)
KUN_COMMENT         VARCHAR2(4000)
KUN_EVT_SMS         VARCHAR2(80)
KUN_EVT_E_MAIL  VARCHAR2(240)
KUN_COMMENT2    VARCHAR2(4000)
KUN_AKTIV_BIS   DATE(7)
KUN_FLG_AGB         VARCHAR2(4)
KUN_TITEL           VARCHAR2(200)

提前感谢您提供线索。

问候

R上。八达

0 个答案:

没有答案