473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL WHERE NOT EXISTS STATEMENT

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!!

Mar 13 '06 #1
1 3206

Why not use HAVING with the count = 1?

Logger wrote:
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!!

Mar 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
5
by: ST | last post by:
Hi, I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to write the syntax to check a sql table to see if the record already exists based on firstname and lastname text fields...
4
by: Jim in Arizona | last post by:
I'm wanting to do a simple controlled voting page. I too our webserver off anonymous and everyone who accesses the website is a domain authenticated user. I've already done some control structure...
4
by: Robin Lawrie | last post by:
I've written an asp form that successfully takes a users firstname, surname, email address, username and password and then add's those details to an Access database. I've been trying to modify...
15
by: MLH | last post by:
Mr Leigh Purvis gave me a very clever piece of SQL to accomplish what is probably an uncommon objective. In it, he uses the EXISTS operator. I can find no documentation on it in A97 HELP. I would...
10
by: Geoff Jones | last post by:
Hi I'm trying to drop a table by using: Dim cmd As New OleDbCommand("DROP TABLE IF EXISTS books", myconnection) cmd.ExecuteNonQuery() but I get a syntax error: "Syntax error in DROP TABLE...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
3
by: Lou | last post by:
Question: I can't seem to get file.exists(filename) to return true when I search using wildcards, and I know there's a file in that dir with that extension. here's the path dim yesno as...
5
by: phillip.s.powell | last post by:
$sql = "SELECT IF((SHOW TABLES LIKE '$subselectTableName'), count(*), NULL) AS numRows FROM $subselectTableName"; I am trying to write a SQL statement that will tell me if a table exists or not,...
3
by: NHM | last post by:
I don't know if it's just me, but DB2 seems to have the worst syntatic parser ever created! Even after triple checking the documentation online, for the syntax of the statements the CLP seems to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.