2015-11-04 119 views
0

請原諒我,如果這看起來有點模糊,並且我的術語不正確。我是一名具有非常有限的SQL查詢經驗的副/初級網絡工程師。除此之外,這是我正在處理的;將兩個SQL查詢合併爲一個

我試圖在Goldmine(CMS/CRM)中創建一個篩選器,篩選下面指定的變量的結果。

WHERE c1.U_COMPANY > ' ' AND c1.U_CONTACT > ' ' AND 
    (c1.U_COUNTRY <> 'SOUTH AFRICA') AND 
    (c1.U_CONTACT NOT LIKE '%MANAGER%' OR c1.U_CONTACT IS NULL) AND  
    (c1.U_KEY1 NOT LIKE '%ARCHIVE%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%DOCUMENT%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%SUPPLIER%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%OTHER%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%TENANT%' OR c1.U_KEY1 IS NULL) 

我需要合併以下內容,但不知道對上述表達式的安全性。

SELECT C1.* 
FROM FMBSM.DBO.CONTACT1 C1 
INNER JOIN (
    SELECT DISTINCT ACCOUNTNO 
    FROM FMBSM.DBO.CONTSUPP 
    WHERE (
      CONTSUPP.RECTYPE = 'P' 
      AND CONTSUPP.CONTACT = 'E-mail Address' 
      ) 
    ) CS 
    ON CS.AccountNo = C1.AccountNo 
ORDER BY C1.CONTACT 

任何意見將不勝感激。

感謝,

羅布

+0

感謝neatening的代碼MiguelH –

+0

不完全確定你的意思,但一個有效的語句以你的'SELECT'部分開頭(不包括'ORDER BY C1.Contact'),然後你把所有的'WHERE'部分放入​​你的'ORDER BY C1.Contact' par t結束。你在使用這個語句的工具是什麼? –

+0

感謝您的編輯Radu –

回答

0

我相信它應該是罰款,把它放在年底,ORDER BY,其中WHERE從句通常放在前:

SELECT C1.* 
FROM FMBSM.DBO.CONTACT1 C1 
INNER JOIN (
    SELECT DISTINCT ACCOUNTNO 
    FROM FMBSM.DBO.CONTSUPP 
    WHERE (
      CONTSUPP.RECTYPE = 'P' 
      AND CONTSUPP.CONTACT = 'E-mail Address' 
      ) 
    ) CS 
    ON CS.AccountNo = C1.AccountNo 
WHERE c1.U_COMPANY > ' ' AND c1.U_CONTACT > ' ' AND 
    (c1.U_COUNTRY <> 'SOUTH AFRICA') AND 
    (c1.U_CONTACT NOT LIKE '%MANAGER%' OR c1.U_CONTACT IS NULL) AND  
    (c1.U_KEY1 NOT LIKE '%ARCHIVE%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%DOCUMENT%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%SUPPLIER%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%OTHER%' OR c1.U_KEY1 IS NULL) AND 
    (c1.U_KEY1 NOT LIKE '%TENANT%' OR c1.U_KEY1 IS NULL) 
ORDER BY C1.CONTACT 
+0

拉杜,我的骨骼和身體中的每一個細小纖維,謝謝你,先生。 我會研究這一點,並確保我知道並理解它的未來。 非常感謝。 –

+0

@Absurd_Introspective這是基本的SQL語法。你可以看看這裏[快速閱讀這些概念](https://technet.microsoft.com/en-us/library/bb264565%28v=sql.90%29.aspx)。 –

+0

這是很棒的信息,謝謝@Radu Gheorghiu –