CREATE TABLE [RS_A] (
[ColA] [varchar] (10)
[ColB] [int] NULL
)
CREATE TABLE [RS_B] (
[ColA] [varchar] (10)
[ColB] [int] NULL
)
INSERT INTO RS_A
VALUES ('hemingway' , 1)
INSERT INTO RS_A
VALUES ('vidal' , 2)
INSERT INTO RS_A
VALUES ('dickens' , 3)
INSERT INTO RS_A
VALUES ('rushdie' , 4)
INSERT INTO RS_B
VALUES ('hemingway' , 1)
INSERT INTO RS_B
VALUES ('vidal' , 2)
I need to find all the rows in A which do not exist in B
by matching on both ColA and ColB
so the output should be
dickens 3
rushdie 4
So if i write a query like this , I dont get the right result set
SELECT A.ColA, A.ColB
FROM RS_A A
INNER JOIN RS_B B
ON A.ColA <B.ColA
OR B.ColB <B.ColB
But if i do the following, i do get the right result, but following
seems convoluted.
SELECT A.ColA, A.ColB
FROM RS_A A
WHERE ColA + CAST(ColB AS VARCHAR)
NOT IN (SELECT ColA+CAST(ColB AS VARCHAR) FROM RS_B B) 6 4005
Here are two approaches. I personally prefer the EXISTS version.
SELECT A.ColA, A.ColB
FROM RS_A A
WHERE NOT EXISTS
(select * from RS_B B
where A.ColA = B.ColA
and B.ColB = B.ColB)
SELECT A.ColA, A.ColB
FROM RS_A A
LEFT OUTER JOIN RS_B B
ON A.ColA = B.ColA
AND B.ColB = B.ColB
WHERE B.ColA IS NULL
Roy Harvey
Beacon Falls, CT
On Fri, 08 Jun 2007 06:36:17 -0700, rs*********@gmail.com wrote:
>CREATE TABLE [RS_A] ( [ColA] [varchar] (10) [ColB] [int] NULL )
CREATE TABLE [RS_B] ( [ColA] [varchar] (10) [ColB] [int] NULL )
INSERT INTO RS_A VALUES ('hemingway' , 1) INSERT INTO RS_A VALUES ('vidal' , 2) INSERT INTO RS_A VALUES ('dickens' , 3) INSERT INTO RS_A VALUES ('rushdie' , 4)
INSERT INTO RS_B VALUES ('hemingway' , 1) INSERT INTO RS_B VALUES ('vidal' , 2)
I need to find all the rows in A which do not exist in B by matching on both ColA and ColB
so the output should be dickens 3 rushdie 4
So if i write a query like this , I dont get the right result set
SELECT A.ColA, A.ColB FROM RS_A A INNER JOIN RS_B B ON A.ColA <B.ColA OR B.ColB <B.ColB But if i do the following, i do get the right result, but following seems convoluted.
SELECT A.ColA, A.ColB FROM RS_A A WHERE ColA + CAST(ColB AS VARCHAR) NOT IN (SELECT ColA+CAST(ColB AS VARCHAR) FROM RS_B B)
Here are two different approaches:
-- SQL Server 2005 only.
SELECT ColA, ColB FROM RS_A
EXCEPT
SELECT ColA, ColB FROM RS_B;
-- SQL Server 2000.
SELECT ColA, ColB FROM
( SELECT 'A' AS Source, ColA, ColB
FROM RS_A
UNION ALL
SELECT 'B' AS Source, ColA, ColB
FROM RS_B ) AS T
GROUP BY ColA, ColB
HAVING COUNT(*) = 1 AND MIN(Source) = 'A'
HTH,
Plamen Ratchev http://www.SQLStudio.com
Roy & Plamen :
Thank for you for you valuable inputs.
- RS
SELECT A.ColA, A.ColB
FROM RS_A A
LEFT OUTER JOIN RS_B B
ON A.ColA = B.ColA
AND B.ColB = B.ColB
WHERE B.ColA IS NULL
Hi Harvey :
I was implementing this solution at another place and i mentioned an
AND instead of a WHERE. and it did not work.
So i used WHERE and viola, things worked.
But actually how can i use a WHERE Clause?
I thought WHERE Clauses were for the FROM table and not for the tables
that are joined to the FROM table
Is my assumption correct ? Obviously it is wrong,
So is it a normal thing to mention conditions where only one table is
needed(like in the above example)
in the WHERE Clause, as then i can do that will all my other queries
where i am joining, and i always have mentioned unique conditions
pertaining to that table alone, using an AND clause.
thank you in advance
RS
The entire idea of using LEFT OUTER JOIN with a NULL test to give the
same results as NOT EXISTS is based on the fact that the WHERE clause
executes after the JOIN processing.
JOINs occurs before anything else. The WHERE clause acts on the
result of the JOIN. If you changed the WHERE to an AND in the example
shown, the test B.ColA IS NULL would move into the ON clause and
become part of the JOIN. Inside the JOIN that column will never be
NULL.
Personally I much prefer the NOT EXISTS syntax over this approach. I
think it shows what you are trying to retrieve - rows in one table
without matches in the other table - more clearly.
Roy Harvey
Beacon Falls, CT
On Fri, 22 Jun 2007 04:48:02 -0700, rs*********@gmail.com wrote:
SELECT A.ColA, A.ColB
FROM RS_A A
LEFT OUTER JOIN RS_B B
ON A.ColA = B.ColA
AND B.ColB = B.ColB
WHERE B.ColA IS NULL
Hi Harvey :
I was implementing this solution at another place and i mentioned an AND instead of a WHERE. and it did not work. So i used WHERE and viola, things worked. But actually how can i use a WHERE Clause? I thought WHERE Clauses were for the FROM table and not for the tables that are joined to the FROM table
Is my assumption correct ? Obviously it is wrong, So is it a normal thing to mention conditions where only one table is needed(like in the above example) in the WHERE Clause, as then i can do that will all my other queries where i am joining, and i always have mentioned unique conditions pertaining to that table alone, using an AND clause.
thank you in advance RS
Thank you for your explanation.
Now, i understand how the WHERE and AND works and can use it much more
effectively in my statements.
RS This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jim Mitchell |
last post by:
So that I can find the pageindex in my datagrid, I need to know the index in
a collection of rows where based on a record id. Right now, I have to loop
through the entire collection.
Is there a...
|
by: robert |
last post by:
running 8.1.7 server, 8.1.6 client.
i *thought* inner join should not return nulls, but not only that,
but i get way more rows than i'm expecting.
assume:
order table:
order_number
|
by: laclac01 |
last post by:
I have developed my own copy function for coping my own dynamic memory
structure. It works, but I feel its not too efficient. There must be
a quicker way to copy the data. In some of the...
|
by: Joel Hedlund |
last post by:
Hi all!
I use python for writing terminal applications and I have been bothered
by how hard it seems to be to determine the terminal size. What is the
best way of doing this?
At the end I've...
|
by: Martin Joergensen |
last post by:
Hi,
I have some files which has the following content:
0 0 0 0 0 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0
|
by: Tony |
last post by:
The w3schools HTML tag reference for <textarea>
http://www.w3schools.com/tags/tag_textarea.asp
says that the attributes 'cols' and 'rows' are REQUIRED attributes for
the textarea tag. Looking at...
|
by: lethek39 |
last post by:
Hey
I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
|
by: Yas |
last post by:
Hello,
I have 2 tables, Table1 and Table2. I have copied all data from Table1
to Table2.
However Table1 is dynamic it has new rows added and some old rows
modified everyday or every other...
|
by: MiziaQ |
last post by:
Hey, I'm using the following code to filter an msflexgrid to only display
rows with the entry "East Cork" in the 8th column. The table has many more entries, so, when I run the application, many...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |