Increment a number in an SQL query

Asked

Viewed 4,528 times

1

I have the appointment that returns the results. I would like to add an incremental number to show the records from 1 to the end, as if it were a new column.

select *, sum(valor) as ValorSoma, count(pedido) as QtdPedido from tb_vendas
where idvendas > 0
and data_venda between '05/10/2015' and '11/04/2016'
and vendedor =’’
group by cliente 
order by QtdPedido desc

2 answers

4

  • 1

    Note: I saw that I had an identical question only for another bank (Postgre), but even though the answer was the same I assumed it would be correct not to mark this question as duplicate and answer.

-1

Guys, after a lot of searching and not finding what I needed, the best solution for me was the following:

-- declarar uma variavel
DECLARE @counter int
-- Definir um numero inicial para o contador
SET @counter = (SELECT MAX(id)+1 AS LAST_EMP FROM [schema].[DBO].[table])

-- somar as linhas com o valor definido
SELECT   (ROW_NUMBER() OVER(ORDER BY r.id DESC) + @counter) ROW
from table r
group by r.id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.