/****** Script for SelectTopNRows command from SSMS ******/
SELECT *,isnull(pvt1.id,0) FROM(
SELECT id,date,relay_type,line_number,machine_number
,process,TIMES,time_val
FROM
[OutputMonitoring].[dbo].[output_tbl]
UNPIVOT(
time_val FOR times
IN
([0000HR],
[0100HR])
) AS PV1
WHERE machine_number = 'CVR 9' and date = '2022-07-14'
)tb2
PIVOT (
max(time_val)
FOR TIMES IN ([0000HR],
[0100HR])
)pvt1
FULL JOIN target_tbl
on
target_tbl.relay_type = pvt1.relay_type
and target_tbl.line_no = pvt1.line_number
and target_tbl.machine_count = pvt1.machine_number
and target_tbl.process = pvt1.process
and target_tbl.Date = pvt1.date
WHERE pvt1.date = '2022-07-14'