Link to home
Start Free TrialLog in
Avatar of Member_2_7966113

asked on

SQL Query Modification

Hello Experts,

Can someone help modify a SQL script.

The following script will compare columns A B C from TableA and TableB and if it sees a match will output the rows as shown in the following image.
User generated image
select convert(VARCHAR,0) Is_Deleted, A.A, A.B, A.C, A.D, A.E, A.F
from TableA A
inner join TableB B on B.A = A.A and B.B = A.B and B.C = A.C
union all
select convert(VARCHAR,1) Is_Deleted, B.A, B.B, B.C, B.D, B.E, B.F
from TableB B
inner join TableA A on A.A = B.A and A.B = B.B and A.C = B.C
order by A, B, C

However, I would like the code modified. I still need sql to find a match on columns A B C but only produce the same output if Column B have the same record, of say bb. At moment, the query matching if all Columns are the same. So because there is a match of xx PP dd the query is providing an output, but I only need the output if the columns were xx bb and dd.

So I only want an output if TableA and TableB look like the following,

User generated image
User generated image
As you can see there is a match on xx bb dd

Please let me know if you need further information
Avatar of ste5an
Flag of Germany image

I don't really get it, but this is also due to missing table DDL and sample data to test it.

But it sounds like you should look into EXCEPT and INTERSECT instead of UNION as set operators.
Avatar of Member_2_7966113


Hi ste5an,

Thanks for reaching out.

Please find attached the sample data to test it.
I forgot table b
The expected results should look like the following:

User generated image
please, please let me know if you need further information
I have reattached the files as .xls.

I hope this helps
Avatar of PortletPaul
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Portle, thanks for your patience. I'm going to test your suggestion. If it doesn't work, I will try and spell exactly what I would like
Portle, that is exactly what I'm looking for - you're a star.

I'm about to post related question, and just so I don't make the same mistake as I did in the way I phrased this question, can you let me know what you didn't understand about this question.
Absolutely amazing .... thank you so much
PortlePaul, I know this question is closed but I was wondering if I could ask you a related question to the fabulous answer?

As you the columns from this question appear as:

User generated image
However, can you let me know what I would need to do to modify the script with the following new column names@

Is_Deleted      FirstName      Lastname      MiddleName      MaidenName      Country
Well I don't really know what you had in mind when you "converted" to a, b, c, d, e and f
so I can't tell you which one is which

does it work left to right?
a becomes FirstName  
b becomes Lastname    
c becomes MiddleName    
d becomes MaidenName      
e becomes Country
f becomes ???

but honestly how would I know?
Very quick question:

Can you let me know what the following code does from the script you provided?


Open in new window

[b]with cmn as (
    from tableA a
    inner join tableB b on a.a = b.a and a.b = b.b and a.c = b.c
    and a.b = 'bb'
select 0 as is_deleted, a.a, b, c, d, e, f from TableA a inner join cmn on a.a = cmn.a
union all
select 1 as is_deleted, b.a, b, c, d, e, f from TableB b inner join cmn on b.a = cmn.a

Open in new window

See T-SQL Fundamentals from Itizik Ben-Gan.

the first "a" is a table alias (shortcut for the table the column comes from), the second "a" is the column name

with cmn as (

This is creating a "common table expression" which has been named "cmn"

See SQL Server documentation online: WITH common_table_expression

This named common table expression can be treated as if it were a real table for the query beneath it, that is why later the query uses:

... inner join cmn on ...

If you have more basic SQL queries you could start here:
In future may I suggest you do not use column names like   a   b   c   ...
Instead use real column names, it is actually easier for us as well.

I also suggest using real table names.