Task on MS SQL. As an example of my problem, I cite two simple tables: the staffing table and employees. It is necessary to compare employees with staffing.
The peculiarity is that in this example, 3 posts are the same (code 32) and they need to be compared by 3 employees with these posts (code 32). The only links between the tables are the Dolby Code (Dol) and the Division Code (Otdel). The Fam field (Last Name) in the WorkTable table is unique. Id in both tables does not participate in the links, unique for the records.
We have:
Staffing table
DolTable:
Id | Otdel | Dol | Fam
431 | 4819 | 32 | 0
432 | 4819 | 32 | 0
433 | 4819 | 32 | 0
Table of workers
WorkTable:
Id | Otdel | Dol | Fam
1521 | 4819 | 32 | Ivanov
1522 | 4819 | 32 | Sidorov
1523 | 4819 | 32 | Petrov
It is necessary to display only 3 lines like this:
Id | Otdel | Dol | Fam
431 | 4819 | 32 | Ivanov
432 | 4819 | 32 | Sidorov
433 | 4819 | 32 | Petrov
No matter how I tried to make such a sample, it does not work.
Tormented request type:
SELECT * from DolTable as dt LEFT JOIN WorkTable as wt ON ((dt.Dol = wt.Dol)and(dt.Otdel = wt.Otdel)) But while I get 3 lines that repeat three times = 9 lines.
It is necessary to withdraw without repetitions.
In the future, instead of SELECT I want to do Update Fam fields in the DolTable table on the same principle.
I will try to be clearer. Added by:
Staffing table
DolTable:
Dol | Fam
32 | 0
32 | 0
32 | 0
Table of workers
WorkTable:
Dol | Fam | Name |
32 | Ivanov | Ivan |
32 | Sidorov | Sergey |
32 | Petrov | Peter |
It is necessary to display only 3 lines without repetitions like this:
Id | Otdel | Dol | Fam | Name
431 | 4819 | 32 | Ivanov | Ivan
432 | 4819 | 32 | Sidorov | Sergei
433 | 4819 | 32 | Petrov | Peter
The essence of the question is that the SELECT LEFT JOIN does a sample without repeating on the left.
DolTabletable? Posts? Why then is theDolfield there? - DaemonHKINNER JOIN, or when usingLEFT JOINuse theWHERE- DaemonHK selection condition