Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Rebson Mendes

IF usando um resultado de um Select count

Recommended Posts

Olá, boa tarde a todos. Não domino bem o SQL, mas estou precisando criar um código que atualize algumas informações no meu banco. Criei o seguinte código...

 

 

select ctr, count(ctr) as Qtd
from tab00101 where tipoparcela = 'L'
group by ctr
having
COUNT(ctr) = 6
Com esse resultado, ele retornou as informações que eu gostaria, porém, preciso que ele atualize as informações que tem nessa mesma tabela
update tab00101 set sequencia = '1' where numeroparcela = '1' and tipoparcela = 'L'
update tab00101 set sequencia = '2' where numeroparcela = '2' and tipoparcela = 'L'
update tab00101 set sequencia = '3' where numeroparcela = '3' and tipoparcela = 'L'
update tab00101 set sequencia = '4' where numeroparcela = '4' and tipoparcela = 'L'
update tab00101 set sequencia = '5' where numeroparcela = '5' and tipoparcela = 'L'
update tab00101 set sequencia = '6' where numeroparcela = '6' and tipoparcela = 'L'
update tab00101 set sequencia = '7' where numeroparcela = '1' and tipoparcela = 'S'
update tab00101 set sequencia = '8' where numeroparcela = '2' and tipoparcela = 'S'
Preciso que também aconteça o seguinte
select ctr, count(ctr) as Qtd
from tab00101 where tipoparcela = 'L'
group by ctr
having
COUNT(ctr) = 5
update tab00101 set sequencia = '1' where numeroparcela = '1' and tipoparcela = 'L'
update tab00101 set sequencia = '2' where numeroparcela = '2' and tipoparcela = 'L'
update tab00101 set sequencia = '3' where numeroparcela = '3' and tipoparcela = 'L'
update tab00101 set sequencia = '4' where numeroparcela = '4' and tipoparcela = 'L'
update tab00101 set sequencia = '5' where numeroparcela = '5' and tipoparcela = 'L'
update tab00101 set sequencia = '6' where numeroparcela = '1' and tipoparcela = 'S'
update tab00101 set sequencia = '7' where numeroparcela = '2' and tipoparcela = 'S'
update tab00101 set sequencia = '8' where numeroparcela = '3' and tipoparcela = 'S'
e assim por diante... com 4... 3... 2...1... Não consigo montar um IF... Alguém poderia me ajudar?

 

 

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Já resolvi meu problema, obrigado pela ajuda.

 

Segue o código para resolução para quem tiver um problema semelhante

 

with 
    CTE_R as
    (
        select 
            *,
            ROW_NUMBER() OVER(PARTITION BY ctr, codcurso ORDER BY tipoparcela, numeroparcela) as NovaSeq
        from Tab00101
    )

update CTE_R
set sequencia = NovaSeq

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá, boa tarde a todos. Gostaria de saber se alguém conseguiria implementar esse código para funcionar no SQL 2000 (sei que as cte a o row_number não funciona nele, apenas na versão 2005 em diante)

 

with 
    CTE_R as
    (
        select 
            *,
            ROW_NUMBER() OVER(PARTITION BY ctr, codcurso ORDER BY tipoparcela, numeroparcela) as NovaSeq
        from Tab00101
    )

update CTE_R
set sequencia = NovaSeq

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.