I hope I understand your question correctly.
Data Prep
CREATE TABLE entry_log(
key_code nvarchar(20) NULL,
indicator nvarchar(3) NULL,
in_out nvarchar(4) NULL,
date date NULL
)
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-T-1', N'T1', N'IN', CAST(N'2022-08-01' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-T-1', N'T1', N'OUT', CAST(N'2022-08-02' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-T-2', N'T1', N'IN', CAST(N'2022-08-03' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-T-2', N'T2', N'OUT', CAST(N'2022-08-04' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-T-3', N'T2', N'IN', CAST(N'2022-08-05' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-T-3', N'T2', N'OUT', CAST(N'2022-08-06' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-1', N'T1', N'IN', CAST(N'2022-08-07' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-1', N'T1', N'OUT', CAST(N'2022-08-08' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-1', N'T2', N'IN', CAST(N'2022-08-09' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-1', N'T2', N'OUT', CAST(N'2022-08-10' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-2', N'T1', N'IN', CAST(N'2022-08-11' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-2', N'T1', N'OUT', CAST(N'2022-08-12' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-2', N'T2', N'IN', CAST(N'2022-08-13' AS Date))
INSERT entry_log (key_code, indicator, in_out, date) VALUES (N'KEY-L-2', N'T2', N'OUT', CAST(N'2022-08-14' AS Date))
Result Query :
WITH rs_t1 AS
(
SELECT
*,
DENSE_RANK() OVER (PARTITION BY SUBSTRING(key_code,1,LEN(key_code)-CHARINDEX('-',REVERSE(key_code)))
ORDER BY SUBSTRING(key_code,LEN(key_code)-CHARINDEX('-',REVERSE(key_code))+2,LEN(key_code)) ASC) AS RNK
FROM
entry_log
WHERE
indicator='T1'
),
rs_t2 AS
(
SELECT
*,
DENSE_RANK() OVER (PARTITION BY SUBSTRING(key_code,1,LEN(key_code)-CHARINDEX('-',REVERSE(key_code)))
ORDER BY SUBSTRING(key_code,LEN(key_code)-CHARINDEX('-',REVERSE(key_code))+2,LEN(key_code)) DESC) AS RNK
FROM
entry_log
WHERE
indicator='T2'
)
SELECT
key_code,
indicator,
max([Date IN]) AS [Date IN],
max([Date OUT]) AS [Date OUT]
FROM
(
SELECT
key_code,
indicator,
date AS [Date IN],
NULL AS [Date OUT]
FROM
rs_t1
WHERE
in_out='IN'
AND
rnk=1
UNION
SELECT
key_code,
indicator,
NULL AS [Date IN],
date AS [Date OUT]
FROM
rs_t1
WHERE
in_out='OUT'
and rnk=1
)t1
GROUP BY
key_code,
indicator
UNION
SELECT
key_code,
indicator,
max([Date IN]) AS [Date IN],
max([Date OUT]) AS [Date OUT]
FROM
(
SELECT
key_code,
indicator,
date AS [Date IN],
NULL AS [Date OUT]
FROM
rs_t2
WHERE
in_out='IN'
AND
rnk=1
UNION
SELECT
key_code,
indicator,
NULL AS [Date IN],
date AS [Date OUT]
FROM
rs_t2
WHERE
in_out='OUT'
and rnk=1
)t2
GROUP BY
key_code,
indicator
ORDER BY [Date IN]
Output
