2010-05-11 58 views
0
SELECT ['VISA CK - 021810$'].[ACCT NBR  #1], ['VISA CK - 021810$'].[ALT CUST NM #1], ['VISA CK - 021810$'].[LAST USED] 
FROM ['VISA CK - 021810$'] 
WHERE ['VISA CK - 021810$'].[ALT CUST NM #1] 
    IN (
     SELECT ['VISA CK - 021810$'].[ALT CUST NM #1] 
     FROM ['VISA CK - 021810$'] 
     GROUP BY ['VISA CK - 021810$'].[ALT CUST NM #1] 
      HAVING COUNT(['VISA CK - 021810$'].[ALT CUST NM #1]) > 1 
     ) 
    AND ['VISA CK - 021810$'].[ACCT NBR  #1] 
    IN (
     SELECT ['VISA CK - 021810$'].[ACCT NBR  #1] 
     FROM ['VISA CK - 021810$'] 
     GROUP BY ['VISA CK - 021810$'].[ACCT NBR  #1] 
      HAVING COUNT(['VISA CK - 021810$'].[ACCT NBR  #1]) > 1 
     ) 
+7

像這樣的列名應該是非法的:) – 2010-05-11 19:11:49

+0

看起來像它會,但我同意尼克,它肯定是痛苦的閱讀。當你嘗試過它發生了什麼? – MJB 2010-05-11 19:13:21

+0

約定!從另一個應用程序導入.csv的結果... – TheDudeAbides 2010-05-11 19:13:36

回答

0

使用改寫聯接:

SELECT t.[ACCT NBR  #1], 
     t.[ALT CUST NM #1], 
     t.[LAST USED] 
    FROM ['VISA CK - 021810$'] t 
    JOIN (SELECT ['VISA CK - 021810$'].[ALT CUST NM #1] 
      FROM ['VISA CK - 021810$'] 
     GROUP BY ['VISA CK - 021810$'].[ALT CUST NM #1] 
     HAVING COUNT(['VISA CK - 021810$'].[ALT CUST NM #1]) > 1) x ON x.[ALT CUST NM #1] = t.[ALT CUST NM #1] 
    JOIN (SELECT ['VISA CK - 021810$'].[ACCT NBR  #1] 
      FROM ['VISA CK - 021810$'] 
     GROUP BY ['VISA CK - 021810$'].[ACCT NBR  #1] 
     HAVING COUNT(['VISA CK - 021810$'].[ACCT NBR  #1]) > 1) y ON y.[ACCT NBR  #1] = t.[ACCT NBR  #1] 

確保它返回你所期望的數據 - 後調。假設你提供的查詢是正確的,JOIN通常比子查詢更受歡迎,但審查和比較執行計劃會告訴你什麼是最適合你的情況。

0

這是一個有效的SQL結構,但我們無法評論它是否有效。

你的意思是:在['VISA CK - 021810$']每一行,返回該行,如果我找到他們相對都[ACCT NBR #1][ALT CUST NM #1] IN子句

0

是的,它會返回良好的數據匹配。 WHERE子句將確保在使用AND時返回任何數據之前,兩個IN子句都受到尊重。

+0

這是我第一次用AND來使用多個IN。謝謝你澄清。 – TheDudeAbides 2010-05-11 19:16:54

相關問題