R
To achieve what you want, you can use the analytical function https://docs.microsoft.com/es-gt/sql/t-sql/functions/lag-transact-sql?view=sql-server-2012 (available from SQL Server 2012). With this function, you can See the value of any of the previous rows, in this case, we will compare the value of the current row category to the category of the previous row, if it is different, we return the category, and if it is equal, we return an empty chain. With this, we made the effect of Print each category once.Sounds more complicated than it is:with
Categorias as (
select 1 idCategoria, 'Coordinación' Categoria
union all select 2, 'Precisión'
union all select 3, 'Esfuerzo'
)
,
Puntuadores as (
select 5 idPuntuador, 1 idCategoria, 'Muy leve' Nombre, 'Descripcion 5' Descripcion, 2 Puntos
union all select 6, 1, 'Leve', 'Descripcion 6', 7
union all select 7, 1, 'Promedio', 'Descripcion 7', 13
union all select 8, 1, 'Considerable', 'Descripcion 8', 18
union all select 9, 1, 'Extremo', 'Descripcion 9', 25
union all select 10, 2, 'Suelto', 'Descripcion 10', 8
union all select 11, 2, 'Cerca', 'Descripcion 11', 10
union all select 12, 2, 'Apretado', 'Descripcion 12', 16
union all select 13, 2, 'Exacto', 'Descripcion 13', 22
union all select 14, 3, 'Poco o leve', 'Descripcion 14', 2
union all select 15, 3, 'Promedio', 'Descripcion 15', 4
union all select 16, 3, 'Apreciable', 'Descripcion 16', 6
)
select case
when c.Categoria = coalesce(lag(c.Categoria) over (order by c.Categoria, p.idPuntuador), 'n/a') then ''
else c.Categoria
end Categoria
, p.idPuntuador
, p.Nombre
, p.Descripcion
, p.Puntos
from Categorias as c
inner join Puntuadores p on p.idCategoria = c.idCategoria
order by c.Categoria, p.idPuntuador
Which produces the following result:Categoria idPuntuador Nombre Descripcion Puntos
------------ ----------- ------------ -------------- -----------
Coordinación 5 Muy leve Descripcion 5 2
6 Leve Descripcion 6 7
7 Promedio Descripcion 7 13
8 Considerable Descripcion 8 18
9 Extremo Descripcion 9 25
Esfuerzo 14 Poco o leve Descripcion 14 2
15 Promedio Descripcion 15 4
16 Apreciable Descripcion 16 6
Precisión 10 Suelto Descripcion 10 8
11 Cerca Descripcion 11 10
12 Apretado Descripcion 12 16
13 Exacto Descripcion 13 22
(12 rows affected)
In detail:lag(c.Categoria) over (order by c.Categoria, p.idPuntuador) returns the value of the column Categoría in the previous row, ordered by c.Categoría e idPuntuadorcoalesce(), if the value of the previous row is null (thing that happens in the first row, because there is no previous row), we return 'n/a'. If it is not null, we return the value of the column in the previous row.compared this value to the value of c.Categoria in the current row. If it's the same, we'll return ''. If it's not the same, we'll return c.Categoria