'SQL Query with 2 conditions but from different tables
I have the below query that has only one condition, which is if the companies are included in the table, then mark them as "Yes" under [Competitor] column, if they are not there' then mark them as "No". What I need is to add to this query another condition, but it's tricky. First is to join another column [Team Name] from a different table "Employees" in order to add the second condition which is: If the [Team Name] from table Employees is 'XYZ' or 'YZA' and [Company Name] = ('XXX','YYY','ZZZ') then set the [Competitor] as 'Yes' else 'No'
Here's the current query I have:
Update [Production].dbo.HR_Companies
Set [Competitor]=Case when [Company Name] in ('XXX','YYY','ZZZ')
then 'Yes'
else 'No' END
The other table name that I need to join the other 2 columns is [Production].dbo.HR_Employees
Any suggestions?
Desired result:
Company_Name| Team_Name| Competitor||
------------ --------- ----------
XXX| XYZ| Yes
YYY| YZA| Yes
ZZZ| XYZ| Yes
YYY| EDF| No
BBB| GBC| No
CCC| RRU| No
DDD| BNN| No
YYY| XYZ| Yes
Company Table
Company Name |
---|
XXX |
YYY |
ZZZ |
AAA |
BBB |
CCC |
DDD |
OOO |
RRR |
VVV |
Employee Table:
Team_Name | Company_Name |
---|---|
XXX | XYZ |
YYY | YZA |
ZZZ | XYZ |
YYY | EDF |
BBB | GBC |
CCC | RRU |
DDD | BNN |
YYY | XYZ |
RRR | XYZ |
VVV | YZA |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|