I am not able to group by with substring
select A.tipodoc,SUBSTRING(A.NumDocExterno,1,7) as JoRef,A.entidade,b.PrecoLiquido,B.TotalIva from CabecCompras