Solved

Query to find out records with non-duplicate of two columns combination

Posted on 2014-03-08
7
216 Views
Last Modified: 2014-03-17
This is an extension for question:
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_28383519.html#a39914320

I have two tables that have the same structure but data might be slightly different. Table names are a and b and column names to be used are c1, c2 (there are many other columns for table a or b)

Example:
select c1, c2 from a
c1 c2
2  UK
3  USA
5  Japan

select c1, c2 from b:
c1  c2
2   UK
3   USA
5   France

After the query, result should be (retrieve c1, c2 info from table a)
c1 c2
5 Japan

Again, please note: I need to compare combined fields of c1 + c2. It's assumed both table a and b have the same number of records and mostly identical
0
Comment
Question by:heyday2004
7 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 200 total points
ID: 39915180
SELECT a.c1, a.c2 FROM a WHERE a.c1 NOT IN (
SELECT a.c1 
    FROM a INNER JOIN b 
        ON a.c1=b.c1 AND a.c2 = b.c2 )

Open in new window


Basically select records that were not selected in the previous question.

HTH,
Dan
0
 

Author Comment

by:heyday2004
ID: 39915446
Thanks, but seems not exactly working as requested. I needs to compare combo of c1 and c2, seems above query didn't list all the occurrences.
0
 
LVL 4

Assisted Solution

by:rshq
rshq earned 100 total points
ID: 39915477
Hi
  I think we have some blank in data in c1,c2 fields.
  please use LTRIM,RTRIM function in  "Dan Craciun" query
SELECT a.c1, a.c2 FROM a WHERE Ltrim(Rtrim(a.c1)) NOT IN (
SELECT Ltrim(Rtrim(a.c1)) 
    FROM a INNER JOIN b 
        ON Ltrim(Rtrim(a.c1))=Ltrim(Rtrim(b.c1)) AND Ltrim(Rtrim(a.c2)) = Ltrim(Rtrim(b.c2)) )

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 100 total points
ID: 39916447
SELECT	a.c1
,	a.c2
FROM	a
LEFT
JOIN	b	ON	a.c1 = b.c1
		AND	a.c2 = b.c2
WHERE	b.c1 IS NULL

Open in new window

0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 39921066
select c1, c2 from a
where not exists
(select 1 from b
 where a.c1 = b.c1
 and a.c2 = b.c2);
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 200 total points
ID: 39921527
Can you please paste/attach samples of the data that was not listed?

We all gave queries that work perfectly with your sample from OP (tested and double tested). We need to see actual data if we're to correct the queries.

Dan
0
 

Author Closing Comment

by:heyday2004
ID: 39936087
Sorry for the late reply. Thanks for all the help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query 8 49
SQL Pivot Rows To Columns 10 53
Help with query 3 23
TSQL - How to declare table name 26 29
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikipedia.org/wiki/PHP  Very powerful.  But a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question