select fio, iif(tovar_type = 1, t3.tovar_name, 0) TOVAR1_NAME, iif(tovar_type = 1, t2.kolvo, 0) TOVAR1_KOLVO, iif(tovar_type = 2, t3.tovar_name, 0) TOVAR2_NAME, iif(tovar_type = 2, t2.kolvo, 0) TOVAR2_KOLVO, iif(tovar_type = 3, t3.tovar_name, 0) TOVAR3_NAME, iif(tovar_type = 3, t2.kolvo, 0) TOVAR3_KOLVO, iif(tovar_type = 4, t3.tovar_name, 0) TOVAR4_NAME, iif(tovar_type = 4, t2.kolvo, 0) TOVAR4_KOLVO, iif(tovar_type = 5, t3.tovar_name, 0) TOVAR5_NAME, iif(tovar_type = 5, t2.kolvo, 0) TOVAR4_KOLVO, iif(tovar_type = 6, t3.tovar_name, 0) TOVAR6_NAME, iif(tovar_type = 6, t2.kolvo, 0) TOVAR6_KOLVO from table2 t2 join table1 t1 on t2.table1_id = t1.table1_id join table3 t3 on t2.table3_id = t3_table3_id |