SELECT task.[ID],
task.[OPEN_TIME],
task.[TIME_BREACH],
task.[PRIORITY],
task.[IS_MASS],
task.[IS_VSP],
task.[REASSIGN],
task.[LOCATION_FULL_NAME],
task.[DESCRIPTION],
task.[AUDIT_WORKGROUP],
task.[SERVICE],
task.[CONTACT_NAME],
mobile.[MOBILE],
phone.[PHONE],
email.[EMAIL],
task.[LOCATION],
iparm.[IP_ARM],
ipmfu.[IP_MFU],
level.[LEVEL],
additionally.[OPERATION_TIME],
domain.[DOMAIN],
pcname.[PC_NAME],
task.[INFO],
comment.[COMMENT],
uz.[UZ],
task.[SOLUTION],
CASE WHEN task.[INFO] LIKE '% вирус%' OR comment.[COMMENT] LIKE '% вирус%' THEN 1 ELSE 0 END VIRUS,
CASE WHEN LOWER(task.PRIORITY) LIKE '%высокий%' OR IS_MASS = 't' OR LOWER(task.PRIORITY) LIKE '%очень важно%' OR LOWER(task.PRIORITY) LIKE '%критический%' OR LOWER(task.PRIORITY) LIKE '%широкомасштабный%' THEN 5
WHEN LOWER(task.SOLUTION) LIKE '%#mos%' OR LOWER(task.SOLUTION) LIKE '%масс_раб%' OR LOWER(task.SOLUTION) LIKE '%мас_раб%' THEN 4
WHEN LOWER(task.INFO) LIKE '%ЕР%' OR LOWER(task.INFO) LIKE '% вирус%' OR LOWER(task.INFO) LIKE '% массов%' OR LOWER(task.PRIORITY) LIKE '%высокий%' OR IS_MASS = 't' THEN 3
WHEN LOWER(task.INFO) LIKE '% все%' OR LOWER(task.INFO) LIKE '%ни у кого%' THEN 2
WHEN task.ID IN (SELECT id_task FROM Priority_task_logs WHERE tag = '#РГК') THEN 1
ELSE 0 END AS [ORDER_PRIORITY]
FROM [Task_on_null] task
LEFT JOIN (
SELECT [SELECTED_VAL] AS [MOBILE], [ID_TASK]
FROM [Shablons_DopParametrs]
WHERE [LABEL] LIKE '%Мобильный телефон%'
)
mobile ON task.[ID] = mobile.[ID_TASK]
LEFT JOIN (
SELECT [SELECTED_VAL] AS [PHONE], [ID_TASK]
FROM [Shablons_DopParametrs]
[LABEL] LIKE '%Внутренний телефон%'
)
phone ON task.ID = phone.[ID_TASK]
LEFT JOIN [portal_SMREP_loc] email ON task.[CONTACT_NAME] = email.[CONTACT_NAME]
LEFT JOIN (
SELECT [SELECTED_VAL] AS [IP_ARM], [ID_TASK]
FROM [Shablons_DopParametrs]
WHERE [LABEL] LIKE '%IP-адрес текущего АРМ%'
)
iparm ON task.ID = iparm.ID_TASK
LEFT JOIN (
[SELECTED_VAL] AS IP_MFU, [ID_TASK]
FROM [Shablons_DopParametrs]
WHERE [LABEL] LIKE '%IP адрес принтера/МФУ%'
)
ipmfu ON task.ID = ipmfu.ID_TASK
// ....... тут еще куча JOIN'ов однотипных
LEFT JOIN (
[SELECTED_VAL] AS UZ, [ID_TASK]
FROM [Shablons_DopParametrs]
WHERE [LABEL] LIKE '%Учётная запись%' OR [LABEL] LIKE '%Логин%'
)
uz ON task.ID = uz.ID_TASK
ORDER BY [ORDER_PRIORITY] DESC