A
A
AleksMo2020-08-04 13:35:06
SQL Server
AleksMo, 2020-08-04 13:35:06

How to query related tables in Microsoft SQL Server so that the result is displayed in one column?

3 linked tables SQL 2017, Position - MainTabMatrix, Tasks - TasksM, Functions - Duties.

Diagram
5f2938b019f0a229116722.jpeg

Directory
5f2938d86fc6f717510202.jpeg

How to make a query that outputs:
Должность: Начальник РЭС
Задача: Метрологическое обеспечение РЭС
Функции: 4.4.1 Контролирует ответственного за учет, состояние и применение вспомогательных (переносных) средств измерения в РЭС.
4.4.2 Организует обеспечение единства и требуемой точности измерений в РЭС
4.4.3 Организует ведение базы данных средств измерений в подсистеме, находящихся в эксплуатации структурного подразделения или на обслуживаемом оборудовании.

It turned out like this, but you need in one column
5f29393a2eb4e855376745.jpeg

Select name_site, NameTask, NameDuties 
  from MainTabMatrix u 
     JOIN TasksM b ON u.main_id = b.main_id 
     JOIN Duties p ON b.id_task = p.id_task
 WHERE u.name_site = 'Начальник РЭС';

<br />
5f29395a1d1b5692927641.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2020-08-04
@AleksMo

SELECT main_id AS [Код], 0 AS [Код.Код], 0 AS [Код.Код.Код], name_site AS [Текст]
  FROM MainTabMatrix
  WHERE name_site = 'Начальник РЭС'
UNION
SELECT MainTabMatrix.main_id, id_taks, 0, NameTask AS [Текст]
  FROM MainTabMatrix
    JOIN TasksM ON MainTabMatrix.main_id = TasksM.main_id
  WHERE name_site = 'Начальник РЭС'
UNION
SELECT MainTabMatrix.main_id, TasksM.id_taks, duties_id, NameDuties AS [Текст]
  FROM MainTabMatrix
    JOIN TasksM ON MainTabMatrix.main_id = TasksM.main_id
    JOIN Duties ON TasksM.id_task = Duties.id_task
  WHERE name_site = 'Начальник РЭС'
ORDER BY 1, 2, 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question