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.
output
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,

TableA
ta
TableB
tb
As you can see there is a match on xx bb dd

Please let me know if you need further information
LVL 1
Member_2_7966113Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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.
Member_2_7966113Author Commented:
Hi ste5an,

Thanks for reaching out.

Please find attached the sample data to test it.
TableA.csv
Member_2_7966113Author Commented:
I forgot table b
TableB.csv
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Member_2_7966113Author Commented:
The expected results should look like the following:

expected
Member_2_7966113Author Commented:
please, please let me know if you need further information
Member_2_7966113Author Commented:
I have reattached the files as .xls.

I hope this helps
TableA.xls
TableB.xls
PortletPaulEE Topic AdvisorCommented:
but only produce the same output if Column B have the same record, of say bb

Honestly I do not understand what you mean by those words. Perhaps it means this? see line 6 in particular
with cmn as (
    select
            a.a
    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


+---+------------+----+----+----+-----+-----+----+
|   | is_deleted | a  | b  | c  |  d  |  e  | f  |
+---+------------+----+----+----+-----+-----+----+
| 1 |          0 | xx | bb | dd | TYH | ADF | AD |
| 2 |          1 | xx | bb | dd | TYH | BIG | UP |
+---+------------+----+----+----+-----+-----+----+

Open in new window

CREATE TABLE tableA(
   A VARCHAR(3) NOT NULL PRIMARY KEY
  ,B VARCHAR(3) NOT NULL
  ,C VARCHAR(3) NOT NULL
  ,D VARCHAR(3) NOT NULL
  ,E VARCHAR(3) NOT NULL
  ,F VARCHAR(3) NOT NULL
);
INSERT INTO tableA(A,B,C,D,E,F) VALUES ('xx','bb','dd','TYH','ADF','AD');
INSERT INTO tableA(A,B,C,D,E,F) VALUES ('HJA','FAD','DAD','YUJ','AA','QWE');
INSERT INTO tableA(A,B,C,D,E,F) VALUES ('AD','DT','GYT','UU','ST','SF');
INSERT INTO tableA(A,B,C,D,E,F) VALUES ('rf','gsd','rq','jn','re','AD');

CREATE TABLE tableB(
   A VARCHAR(2) NOT NULL PRIMARY KEY
  ,B VARCHAR(3) NOT NULL
  ,C VARCHAR(3) NOT NULL
  ,D VARCHAR(3) NOT NULL
  ,E VARCHAR(3) NOT NULL
  ,F VARCHAR(3) NOT NULL
);
INSERT INTO tableB(A,B,C,D,E,F) VALUES ('xx','bb','dd','TYH','BIG','UP');
INSERT INTO tableB(A,B,C,D,E,F) VALUES ('AD','ADF','NH','OL','NM','GU');
INSERT INTO tableB(A,B,C,D,E,F) VALUES ('FH','IJJ','GYT','GGT','ST','HDF');
INSERT INTO tableB(A,B,C,D,E,F) VALUES ('tg','ar','afa','ar','af','af');

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Member_2_7966113Author Commented:
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
Member_2_7966113Author Commented:
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.
Member_2_7966113Author Commented:
Absolutely amazing .... thank you so much
Member_2_7966113Author Commented:
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:

columns
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
PortletPaulEE Topic AdvisorCommented:
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?
Member_2_7966113Author Commented:
Very quick question:

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

select
            a.a

Open in new window

[b]with cmn as (
    select
            a.a[/b]
    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

ste5anSenior DeveloperCommented:
See T-SQL Fundamentals from Itizik Ben-Gan.
PortletPaulEE Topic AdvisorCommented:
a.a

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:
https://docs.microsoft.com/en-us/sql/t-sql/queries/queries?view=sql-server-2017
PortletPaulEE Topic AdvisorCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.