SELECT [ФИО], 1 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 [ФИО],
DAY([Событие]) AS [День],
FORMAT([Событие], 'HH:mm') AS [Время]
FROM [Проходная]
WHERE FORMAT([Событие], 'yyyy-MM') = @P) AS enDay
PIVOT ( MIN([Время])
FOR [День] 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 [ФИО], 2,
[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 [ФИО],
DAY([Событие]) AS [День],
FORMAT([Событие], 'HH:mm') AS [Время]
FROM [Проходная]
WHERE FORMAT([Событие], 'yyyy-MM') = @P) AS enDay
PIVOT ( MAX([Время])
FOR [День] 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 [ФИО], 3,
[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 [ФИО],
DAY([Событие]) AS [День],
FORMAT(MAX([Событие]) - MIN([Событие]), 'HH:mm') AS [Время]
FROM [Проходная]
WHERE FORMAT([Событие], 'yyyy-MM') = @P
GROUP BY [ФИО], DAY([Событие])) AS enDay
PIVOT ( MAX([Время])
FOR [День] 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 1, 2
Список для параметра:
SELECT DISTINCT FORMAT([Событие], 'yyyy-MM')
FROM [Проходная]
ORDER BY 1 DESC
Значение по-умолчанию для параметра:
SELECT FORMAT(MAX([Событие]), 'yyyy-MM')
FROM [Проходная]