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

#018 Concatenate rows in the same column in Azure SQL

Como concatenar linhas na mesma coluna no Azure SQL 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 "f.Combustibleis" table. I'll explain each

part of the code:
  1. SELECT [ESTADO_SIGLA], STRING_AGG([FLAG], ',') AS FLAG

    :
    • select: Indicates that the next part of the code will specify which columns will be returned in the query.
    • [ESTADO_SIGLA]: Select the "ESTADO_SIGLA" column.
    • STRING_AGG([FLAG], ',') THE FLAG: Uses the STRING_AGG function to concatenate the values in the "FLAG" column grouped by "ESTADO_SIGLA", separated by a comma (','). The result is renamed "FLAG".
  2. FROM [f].[ Fuels]

    :
    • FROM: Indicates the table from which the data will be selected.
    • [f]. [Fuels]: 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. GROUP BY ESTADO_SIGLA:
  4. GROUP BY

    • : Groups the results based on the specified column, in this case, "ESTADO_SIGLA". This means that the concatenation will be performed for each distinct group of values in the "ESTADO_SIGLA" column.

Therefore, the code returns a list of state acronyms ("ESTADO_SIGLA") and their flags ("FLAG") concatenated into a single string, separated by a comma. Each line of the output represents a state and its associated flags. This query is useful when you want to get information about the flags associated with each state in a fuel table.

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

  • Reading time 1 min 14 seg

avatar
Fabio Santos

Data Scientist and Consultant for Digital and Analytics Solutions


  • Share

Youtube Channel

@fabioms

Subscribe now