Buenas tardes comunidad.
Solicitando su valiosa ayuda, tengo la siguiente función que lo que hace es enumerar mis partidas de diario, todo esta bien hasta ahí, el problema es que me solicitaron que la numeración se reinicie cada mes y vuelva a contar desde 1, he intentado meter mas código pero no me funciona.
Este es mi código.
create function [dbo].[_SBOF_DetallaNumerodePartida_diario](@numero_transaccion int) returns int as
begin
--variables
DECLARE @NumPartida int = 0
,@Contador int
DECLARE cursor1 CURSOR FOR
select a.TransId, count(1)
FROM OJDT a
inner join JDT1 b on b.TransId = a.TransId
left join OJDT c on c.StornoToTr = a.TransId
group by a.TransId, a.TransType, c.StornoToTr, a.Memo, a.RefDate
having a.TransId between min(a.TransId) and @numero_transaccion
-- and a.RefDate between dateadd(day,1,eomonth(a.RefDate,-1)) and a.RefDate
and a.TransType = 30 and c.[StornoToTr] is null and not(a.[Memo] like '%Anula%')
OPEN cursor1
FETCH NEXT FROM cursor1 into @numero_transaccion, @Contador
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@numero_transaccion) = 1 and isnull(@Contador,0) = 0 BEGIN
FETCH NEXT FROM cursor1 into @numero_transaccion, @Contador
END
ELSE
IF ISNULL(@Contador,0) = 0 BEGIN
FETCH NEXT FROM cursor1 into @numero_transaccion, @Contador
END
else
SET @NumPartida = @NumPartida + 1
FETCH NEXT FROM cursor1 into @numero_transaccion, @Contador
END
close cursor1
DEALLOCATE cursor1
return @NumPartida
END
Espero me puedan ayudar.