Add a column to a table from a norther table depending of a accouple of conditions

Nazdac Nl 1 Reputation point

Hi All 

This is simplifying photo of my DataModel 

I am using SQL management server and I am beginner to SQL
I have to tables , Dim_Table A and Fct_Table B .. the relation is one to many . (its 99% 1:1 relation)

I would like to create a script that can show all columns in Dim table A adding to them Col5 with is resulted by:
is equal Col 2 in Fct_Table B when two condtions are applied : 

  • the first one : PK equal to SK in both tables 
  • the second condition that Col 3 in Fct_table B equal "Text1"

Thanks in advance
Nazdac :)

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
9,792 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ronen Ariely 14,646 Reputation points MVP

    Good day,

    Thanks for your nice drawing. It is clear that you made an effort to ask the question in the right way and elaborate with a lot of text. Unfortunately, images are useless and we do not query images but data in tables and instead of stories about your scenario you could simply provide the way to reproduce it - demo it.

    • Instead of image of tables, you should provide the queries to create the tables.
    • Instead of stories about how the data looks like, you should provide queries to insert some sample data to the tables for the sake of the discussions.
    • and finally you should present what is the expected result-SET according the data you inserted to the tables.
    • In addition you should add a short description of the business rules - how you get the expected result from the data in the demo tables
    • and providing the version of your server will help us to provide a solution that fit your version.

    Please always remember these 5 points which you should provide in any question :-)

    Back to your question...

    What you are asking is to SELECT data from Dim_Table A and JOIN to that data some data from Fct_Table B. The condition for the JOIN is that the data in the PK column in Dim_Table A will be the same as the data in column PK in Fct_Table B

    SQL language is based on simple English and the solution is a query which looks almost like the above sentence:

    SELECT  A.PK, A.Col1, A.Col2, B.Col4   
    FROM Dim_Table A  
    LEFT JOIN Fct_Table B ON A.PK = B.PK  

    Notice that this is only an illustration of your case and not necessarily the exact query which you need, since you did not provided the information to reproduce your scenario (as mentioned above in the 5 points). To get the exact solution you will need to provide the information or better... learn about JOIN.

    Joining tables is one of the most basic task in SQL queries. You should really go over a simple tutorial from start to end, instead of jumping from one question to the other. Learning the entire SQL language should not take more than 3 full days to someone who speak English (children can learn it at the same age that they learn to write in English).

    Here you have a nice tutorial (free online with samples and exercises):

    It is NOT RECOMMENDED but if you are really VERY lazy, then at least get more information about joining data of two tables in order to solve your need in this question, here:

    0 comments No comments