0

I have three tables as below

three tables with the expected result

I want to insert the column dep_typ in table 2 as selecting the column from table 1.

But the dep_typ in table 2 has all 4 values as 'U' and whereas in table 1 it is three times 'U' and 1 time 'F'. I want the result to be as same as table 1.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    well how can you determine which row in table1 for 7314 to use? Or are you expecting 5 rows in table2? You need to edit the question and explain the logic you are trying to apply and the expected output – OldProgrammer Sep 23 '20 at 16:46

1 Answers1

0

The following will join the dep_type values from t1 with only the records from t2. Your data sample does not have any matching ex_line values common to t1 and t2. Is this true of the entire dataset? It if isn't true for the entire dataset, then @OldProgrammer is right - the join will not return the exact number of records in t1.

SELECT
    t1.position,
    t1.ss_id,
    t2.ex_line,
    t1.dep_Typ,
    t1.num
FROM
    t1
       INNER JOIN
          t2
             ON t1.position = t2.position
             AND t1.ss_id = t2.ss_id
                INNER JOIN
                    t3
                         ON t1.ss_id = t3.ss_id
                         AND t1.value = t3.value
                         AND t2.vech_num = t3.vech_num
;
Greg
  • 84
  • 1
  • 1
  • 7