Count records that precede the selected one in the sql server, mysql query

Asked

Viewed 39 times

2

I have a question: How do I know how many records you have before a selected value.

For example:

 Select * from cidades where nome= 'SAO PAULO' ORDER BY nome

Assuming that the cities table has 500 records and I searched for SAO PAULO, I want to know how many cities there are before SAO PAULO in my search, take into account ORDER BY name

  • IS sql-server, postgresql or mysql? The way of doing in each DBMS is different.

  • Preferably sql-server, but I need the 3

  • 1

    Basically is what is in the post linked above, is a SELECT count(*) AS posicao WHERE nome < "São Paulo"

  • 2

    @Diegorafaelsouza see the post I inkei above, has exactly what you said. A sub to find the score (in case it would be the city ID) and another external to rank

  • I managed in Sql-server SELECT COUNT(COD)- 1 AS TOTAL FROM CODMUNICIPIO WHERE CIDADE <= '" + Cidadeempresa + "', Thanks a lot for the tips, helped a lot

No answers

Browser other questions tagged

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