LEFT
, а в основном коде не поменял *тупица*DECLARE @P VARCHAR(7) = FORMAT(@StartDate, 'yyyy-MM')
SELECT enPivot.FullName, enPivot.Name, 'Пришел' AS Line,
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16],
[17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
FROM
( SELECT Employee.FullName,
IncidentOrganizationDictionary.Name,
DAY(Log.DateTime) AS Day,
FORMAT(Log.DateTime, 'HH:mm') AS Time
FROM Log
INNER JOIN Employee ON Employee._id = Log.EmployeeID
INNER JOIN Organization2EmployeeAssignment ON Organization2EmployeeAssignment.EmployeeID = Log.EmployeeID
INNER JOIN IncidentOrganizationDictionary ON IncidentOrganizationDictionary._id = Organization2EmployeeAssignment.OrganizationID
WHERE FORMAT(Log.DateTime, 'yyyy-MM') = @P AND IncidentOrganizationDictionary.Name IS NULL) AS enDay
PIVOT ( MIN(Time)
FOR Day IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
[11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
[21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) AS enPivot
UNION
SELECT enPivot.FullName, enPivot.Name, 'Ушел',
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16],
[17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
FROM
( SELECT Employee.FullName,
IncidentOrganizationDictionary.Name,
DAY(Log.DateTime) AS Day,
FORMAT(Log.DateTime, 'HH:mm') AS Time
FROM Log
INNER JOIN Employee ON Employee._id = Log.EmployeeID
INNER JOIN Organization2EmployeeAssignment ON Organization2EmployeeAssignment.EmployeeID = Log.EmployeeID
INNER JOIN IncidentOrganizationDictionary ON IncidentOrganizationDictionary._id = Organization2EmployeeAssignment.OrganizationID
WHERE FORMAT(Log.DateTime, 'yyyy-MM') = @P AND IncidentOrganizationDictionary.Name IS NULL) AS enDay
PIVOT ( MAX(Time)
FOR Day IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
[11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
[21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) AS enPivot
UNION
SELECT enPivot.FullName, enPivot.Name, 'Отработал',
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16],
[17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
FROM
( SELECT Employee.FullName,
IncidentOrganizationDictionary.Name,
DAY(Log.DateTime) AS Day,
CAST(MAX(Log.DateTime) as datetime) - CAST(MIN(Log.DateTime) as datetime) AS Time
FROM Log
INNER JOIN Employee ON Employee._id = Log.EmployeeID
INNER JOIN Organization2EmployeeAssignment ON Organization2EmployeeAssignment.EmployeeID = Log.EmployeeID
INNER JOIN IncidentOrganizationDictionary ON IncidentOrganizationDictionary._id = Organization2EmployeeAssignment.OrganizationID
WHERE FORMAT(Log.DateTime, 'yyyy-MM') = @P AND IncidentOrganizationDictionary.Name IS NULL
GROUP BY Employee.FullName, IncidentOrganizationDictionary.Name, DAY(Log.DateTime)) AS enDay
PIVOT ( MAX(Time)
FOR Day IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
[11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
[21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) AS enPivot
ORDER BY Line DESC
WHERE FORMAT(Log.DateTime, 'yyyy-MM') = @P AND IncidentOrganizationDictionary.Name IS NULL
, а список не возвращается. Если же пишу IS NOT NULL - всё ок, всех у кого организации есть выводит. Такое ощущение как будто возвращается список только тех, у кого организация указана. Но если я сделаю обычный запрос всех, кто без организации, то выводит FORMAT(MAX([Событие]) - MIN([Событие]), 'HH:mm') AS [Время]
на CAST(MAX(Log.DateTime) as datetime) - CAST(MIN(Log.DateTime) as datetime) AS Time
. Теперь всё работает как надо! Ещё раз спасибо Константин Цветков за решение моей головной боли!