We help IT Professionals succeed at work.

SQL Query Modification

103 Views
Last Modified: 2018-12-22
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

ste5anSenior Developer
CERTIFIED EXPERT

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

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
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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
CERTIFIED EXPERT
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
CERTIFIED EXPERT

Commented:
See T-SQL Fundamentals from Itizik Ben-Gan.
PortletPaulEE Topic Advisor
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.