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
)
views:
64answers:
3
A:
It is a valid SQL construct but we can't comment on whether it works or not.
You are saying: for each row in ['VISA CK - 021810$']
, return this row if I find matches for both [ACCT NBR #1]
and [ALT CUST NM #1]
in their relative IN clauses
gbn
2010-05-11 19:13:57
A:
Yes, it would return good data. The WHERE clause will assure that both IN clauses are respected before returning any data as you're using an AND.
Will Marcouiller
2010-05-11 19:14:45
This is the first time I've used multiple IN's with AND. Thank you for clarifying.
TheDudeAbides
2010-05-11 19:16:54
A:
Rewritten using JOINs:
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]
Make sure it returns the data you expect - tune after that. Assuming the query you provided is correct, JOINs are generally preferred over subqueries but reviewing and comparing the Execution Plan will tell you what is best for your situation.
OMG Ponies
2010-05-11 19:27:21