Click here to Skip to main content
15,885,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:



I have a question

I have two tables table A and table B

Now I want to update a flag as 1 when records are common in both the tables and update flag as 0 when no common records are there . Adding a snippet from my code
Python
dataframe = spark.sql("""select b.* 
                         from ( Select * 
                                from db.A 
                                where source = 'fileFormat.xlsx'
                                ) a
                         left join table B b on a.column1 = b.column1
                         and a.column2 = b.column2
                         and a.column3 = b.column3
                         where b.column2 is not null
                       """)

In layman's language how do i find the common records and update the flags.

What I have tried:

I have tried applying join but that doesn't seem to work
Posted
Updated 15-Mar-22 2:50am
v2
Comments
Meysam Toluie 24-Feb-22 11:30am    
Add some example of data and your expected result to clear up your goal.
Andre Oosthuizen 24-Feb-22 14:49pm    
Do you have any row id's to relate to i.e. xl file column 1 has id, now compare to date from table - if same id, then checked 1?
Maciej Los 25-Feb-22 2:46am    
Sorry, but we can't read in your mind or direct from your screen. If you want our help, you need to explain what you want to achive, what have you done so far and what is you problem. Example data would be helpful too.
At this moment we don't know what table should be updated with new flags. What flag have to be set in case of record exists, etc.

1 solution

You haven't really given enough information for a definitive answer but perhaps this will help.

I created some sample data, with nothing at all in the flag and with only 2 matching records in table B...
SQL
declare @A table (commonColumn int, otherData nvarchar(255), flag bit null);
insert into @A (commonColumn, otherData) values
(1,'sample 1'), (2, 'sample 2'), (3, 'sample 3'), (4,'sample 4');

declare @B table (commonColumn int, otherData nvarchar(255));
insert into @B (commonColumn, otherData) values
(2, 'Matches with sample 2'),(3, 'Matches with sample 3');
The simplest way to update the flag when there are no common records is to posit that we don't think we'll find any, so just set everything to 0 = "no match"
SQL
update @A set flag = 0;
Then update where you do find a match - this should use an inner join so that the data set returned is only the records that appear in both tables
SQL
update a set flag = 1
from @A a 
inner join @B b on a.commonColumn = b.commonColumn
select * from @A;
If you didn't want to set everything to 0 up front this is how you would use a left join to do what you want - note the where clause
SQL
update a set flag = 0
from @A a 
left outer join @B b on a.commonColumn = b.commonColumn
where b.commonColumn is null;
select * from @A;
However, based on the title of your post - here is how to use a case and do it all at once - you still need the left join but the case must do the test for the null instead of the where clause
SQL
update a set flag = case when b.commonColumn is null then 0 else 1 end
from @A a 
left outer join @B b on a.commonColumn = b.commonColumn;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900