SQL WHERE NOT EXISTS STATEMENT

G

Guest

Can someone tell me why my query is not working the way I think/need it to.
I build a #tmp table with columns Company AccountNo, ContactName, Title,
etc in it.
There can be many records with the same company name, different contacts.
Example:
Company ABC 123 John Smith CEO
Company ABC 345 Mary Adams CFO
Company DEF 222 Tony Curtis

On my second #tmp table I need to pull out just one contact per companyName
.. Below is the query I’m using but it’s pulling all records.

--drop table #MainAccounts
CREATE TABLE #MainAccounts(
Company varchar(40)
,accountno varchar(20)
)
INSERT INTO #MainAccounts
SELECT
a.Company
,a.accountno
FROM #GetMains a
WHERE NOT EXISTS ( SELECT b.Company
FROM #MainAccounts b
Where b.Company=a.Company)
Help with suggestions!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top