sql-servercommon-table-expressionself-referencing-table

SELECT inherit values from Parent (self-references)


I have 02 tables Rubrique and BulletinRubrique the table Rubrique contain the following columns :

ID int not null,
Name, varchar(max) not null,
RubriqueA_ID int null,
RubriqueB_ID int null

with data for example :

+-------+---------+--------------+--------------+
| ID    | Name    | RubriqueA_ID | RubriqueB_ID | 
+-------+---------+--------------+--------------+
| 1     | R1      |    2         |    3         |
| 2     | R2      |    1         |    2         |
| 3     | R3      |    NULL      |    NULL      |
| 4     | R4      |    NULL      |    6         |
| 5     | R5      |    6         |    NULL      |
| 6     | R6      |    NULL      |    1         |
+-------+---------+--------------+--------------+

the two columns RubriqueA_ID, RubriqueB_ID are the foreigns Keys for the same table Rubrique (self-referencing) and they might be NULL

For the table BulletinRubrique :

ID int not null,
EmployeID int not null,
RubriqueID int not null,
Value float not null

with data :

+-------+-----------+--------------+------------+
| ID    | EmployeID | Rubrique_ID  | Value      | 
+-------+-----------+--------------+------------+
| B1    | EMP1      | 1            | 150        |
| B1    | EMP1      | 2            | 220        |
| B1    | EMP1      | 3            | 195        |
| B1    | EMP1      | 4            | 170        |
| B1    | EMP1      | 5            | 320        |
| B1    | EMP1      | 6            | 745        |
+-------+-----------+--------------+------------+

What I am trying to do is bulding the sql query to get result as :

+-------+-----------+--------------+------------+----------+---------+
| ID    | EmployeID | Rubrique_ID  | Value      | A_Value  | B_Value |
+-------+-----------+--------------+------------+----------+---------+
| B1    | EMP1      | 1            | 150        | 220      | 195     |
| B1    | EMP1      | 2            | 220        | 150      | 220     |
| B1    | EMP1      | 3            | 195        | NULL     | NULL    |
| B1    | EMP1      | 4            | 170        | NULL     | 745     |
| B1    | EMP1      | 5            | 320        | 745      | NULL    |
| B1    | EMP1      | 6            | 745        | NULL     | 150     |
+-------+-----------+--------------+------------+----------+---------+

Please help me to build this query and thanks


Solution

  • http://rextester.com/FIJE42564 is the working code

    SELECT t2.ID, t2.EmployeID, t2.RubriqueID, t2.Value, t2.A_Value, r2.Value AS B_value  FROM BulletinRubrique r2 
     RIGHT JOIN
       (
        SELECT t1.ID, t1.EmployeID, t1.RubriqueID, t1.Value, r1.VALUE AS A_VALUE, t1.B_VALUE  FROM BulletinRubrique r1 
          RIGHT JOIN ( 
            SELECT b.ID, b.EmployeID,b.RubriqueID, b.Value, r.RubriqueA_ID AS A_Value,  r.RubriqueB_ID AS B_Value
            FROM BulletinRubrique b RIGHT JOIN Rubrique r ON r.ID = b.RubriqueID ) t1 
          ON t1.A_Value =  r1.RubriqueID
       )t2 
          ON t2.B_Value =  r2.RubriqueID
          ORDER BY t2.RubriqueID