with minValue as
(
select id, portid from tbl1 where daynumber = 1
),
maxValue as
(
select id, portid, daynumber from tbl1 where daynumber = (select max(daynumber) from tbl1 t where tbl1.id = t.id)
)
select t1.id, t1.portid, t2.daynumber from minValue t1 inner join maxvalue t2 on t1.id = t2.id and t1.portid = t2.portid
That should work. It gets the minimum and maximum values and only returns situations where the port id is the same on the first and last day.