SQL Query Modification

Member_2_7966113
Member_2_7966113 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.

Author

Commented:
Hi ste5an,

Thanks for reaching out.

Please find attached the sample data to test it.
TableA.csv

Author

Commented:
I forgot table b
TableB.csv
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
The expected results should look like the following:

expected

Author

Commented:
please, please let me know if you need further information

Author

Commented:
I have reattached the files as .xls.

I hope this helps
TableA.xls
TableB.xls
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
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

Author

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

Author

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.

Author

Commented:
Absolutely amazing .... thank you so much

Author

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 Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
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?

Author

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 Developer

Commented:
See T-SQL Fundamentals from Itizik Ben-Gan.
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
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 Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial