Increment a number in a SQL query

1

I have the query that returns me 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
    
asked by anonymous 12.04.2016 / 21:48

2 answers

4

Just use the ROW_NUMBER() command. In your case add the following SELECT ROW_NUMBER() OVER(ORDER BY QtdPedido desc), ...

Source:

link

    
12.04.2016 / 21:57
-1

Well, after much searching and not finding what I needed, the best solution for me was this:

-- 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
    
16.12.2016 / 20:10