I think the following query is more intuitive to understand:
selectcreate maxtable SLVs_Flagged (PARAMETER_NAME varchar(30), GW_LOCATION_ID varchar(30), Report_Result int, DETECT_FLAG char(1) as)
go
insert ReportResultinto dbo.SLVs_Flagged
values ('abc', PARAMETER_NAME'cor1', GW_LOCATION_ID128, 'N'), ('abc', 'cor1', 12, 'Y')
t.DETECT_FLAG, --('def', all'cor1', other500, columns'Y'), can('def', be'cor1', listed50, here'N')
go
;with s as (
select max(Report_Result) as Report_Result, PARAMETER_NAME, GW_LOCATION_ID
from SLVs_Flagged s
cross applygroup (by PARAMETER_NAME, GW_LOCATION_ID)
SELECT *s.Report_Result, s.PARAMETER_NAME, s.GW_LOCATION_ID, t.DETECT_FLAG
FROM SLVs_Flagged t
whereinner join s
on t.PARAMETER_NAME = s.PARAMETER_NAME
and t.GW_LOCATION_ID = s.GW_LOCATION_ID
and t.Report_Result = s.ReportResult
) T
group by s.PARAMETER_NAME, s.GW_LOCATION_IDReport_Result