Skip to content
Advertisement

Return value from another table that matches values from the first table

I have a simple problem. I have tables A and B, both contain 3 common columns (name, id, date), and besides those 3, both tables have completely different set of columns.

Now, I need to relate data from Table B to table A for visualization purposes. Since I am not that good with SQL, I thought I could ask if it’s possible, I know I can have computed columns as I already have one in table A for something simple. However I want to do something like this :

A new column in table A that contains a value of the column “Special” from table B for any row where name, date and ID from table A matches name, date and ID for table B, if no matches leave as blank or null.

Example

Table A:

Table B:

Result in table A:

Is this possible to do in SQL Server?

Advertisement

Answer

Yes, it is possible. But in order to reference another table in a computed column, you have to do it via creating a user-defined function. This will have name,date,id as input parameters and will output a bit if such a combination exists in table B. Something like:

Then, you can use this function in a computed column:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement