Asi que tal?
Código SQL:
Ver originalDECLARE @reg INT
SELECT @reg = COUNT(1)
FROM optimas AS o
INNER JOIN pesos AS p ON o.codigo = p.codoptima
INNER JOIN htargets AS h ON o.codigo = h.codoptima
WHERE o.linea = @linea
AND p.fecha >= @fecha AND h.fecha >= @fecha
AND p.hora = @hora AND h.hora = @hora
ORDER BY fecha
SELECT top 1 h.hora AS hora1,<aqui tus campos>,@reg AS 'RowsReturned'
FROM optimas AS o
INNER JOIN pesos AS p ON o.codigo = p.codoptima
INNER JOIN htargets AS h ON o.codigo = h.codoptima
WHERE o.linea = @linea
AND p.fecha >= @fecha AND h.fecha >= @fecha
AND p.hora = @hora AND h.hora = @hora