Free cookie consent management tool by TermsFeed Policy Generator
  • Azure SQL
  •    13 page views
  •   0 video views
  • 2024, November 07, Thursday

#057 Rank the top 10 values in Azure SQL

Como classificar os 10 maiores valores em Consulta (SELECT) existente na linguagem de Manipulação de Dados (DML) do Azure SQL/SQL Server

This T-SQL code performs a query on the table [f].[ MOEDA_COTACOES] and uses the RANK() sort function to assign a rating to rows based on column [TAXA_VENDA]. I'll explain each part of the code

:SELECT TOP (10) [CURRENCY], [
  1. TAXA_VENDA], RANK() OVER (ORDER BY [TAXA_VENDA] DESC) AS ORDER:select TOP (

    10
    • ): Indicates that only the first 10 results will be returned in the query.
    • [CURRENCY], [TAXA_VENDA]: Select the "CURRENCY" and "TAXA_VENDA" columns.
    • RANK() OVER (ORDER BY [TAXA_VENDA
    • ] DESC) AS ORDER: Uses the RANK() function to assign a rank number to rows based on the descending order of column [TAXA_VENDA]. The result of this classification is renamed to "ORDER".
  2. FROM [f].[ MOEDA_COTACOES]:

    • FROM: Indicates the table from which the data will be selected.
    • [f]. [MOEDA_COTACOES]: This is the name of the table from which the data will be extracted. The prefix "f." suggests that the table is in a schema called "f".
  3. ORDER BY: Sorts

    the
    • results of the query.
    • [ORDER]: Sorts the results based on the "ORDER" column, which is the result of the RANK() function.

Therefore, the code returns the top 10 rows of the table [f]. [MOEDA_COTACOES] with the "CURRENCY" and "TAXA_VENDA" columns, and an additional column called "ORDER" that represents the sort of rows based on the "TAXA_VENDA" column in descending order. This query is useful when you want to identify the best (or worst) currency selling rates in a table.

This content contains
  • Content Imagem
  • Language Portuguese
  • Duration
  • Subtitles Não

  • Reading time 1 min 18 seg

avatar
Fabio Santos

Data Scientist and Consultant for Digital and Analytics Solutions


  • Share

Youtube Channel

@fabioms

Subscribe now