I have 2 tables:
One is Promotion
| PromoId |Promo Decription|
----------------------
| 101 | abc|
| 102 | pqr|
| 103 | alp|
| 104 | adc|
| 201 | abc|
and the other is PromotionType
| PromoId | PromoType |
----------------------
| 101 | 1 |
| 121 | 2 |
| 188 | 3 |
| 104 | 4 |
| 191 | 4 |
| 102 | 4 |
I want a resultant table
| PromoId | Flag |Promo Decription |PromoType |
----------------------
| 101 | 1 | | 1 |
| 121 | 0 | | 2 |
| 188 | 0 | | 3 |
| 104 | 1 | adc | 4 |
| 191 | 0 | | 4 |
| 102 | 1 | pqr | 4 |
| 103 | 1 | alp | |
| 201 | 0 | abc | |
i.e I want a resultant table , which is the union of two tables .It should not contain duplicate values and the value of flag is set to true for all the values of PromoId’s which are common to both tables.
I am using Sql Server as our database.
You can use a
FULL OUTER JOINto perform this:See SQL Fiddle with Demo
Result:
Edit, even with your changes to the data sample the query will still produce the result:
See SQL Fiddle with Demo
Result is: