Si tu servidor es sql este ejemplo (creo) que responde lo que entendi de tu pregunta. Si es otro proveedor podes tener que ajustar la sintaxis pero el ejempo sirve igual.
(los registros con E son Enganche y con A son avono de la misma venta)
create table testventas (
id_tbl int identity(1,1),
id_venta int,
cuota int,
tipo char(1),
importe money)
insert into testventas (id_venta,cuota,tipo,importe) values (1,1,'E',5.8)
insert into testventas (id_venta,cuota,tipo,importe) values (1,2,'E',5.8)
insert into testventas (id_venta,cuota,tipo,importe) values (1,3,'E',5.8)
insert into testventas (id_venta,cuota,tipo,importe) values (2,1,'E',5.8)
insert into testventas (id_venta,cuota,tipo,importe) values (2,2,'E',5.8)
insert into testventas (id_venta,cuota,tipo,importe) values (2,3,'E',5.8)
insert into testventas (id_venta,cuota,tipo,importe) values (2,1,'A',10)
insert into testventas (id_venta,cuota,tipo,importe) values (2,2,'A',10)
select isnull(max(cuota)+1,1) from testventas where id_venta=1 and tipo='E' --Resultado(4)
select isnull(max(cuota)+1,1) from testventas where id_venta=1 and tipo='A' --Resultado(1)
select isnull(max(cuota)+1,1) from testventas where id_venta=2 and tipo='A' --Resultado(3)
Espero qeu te sirva.
Saludos, Mariano |