Returning only duplicated data
So, you want to show only rows that there is more than one with same table and column name.

We can use COUNT(*) OVER (PARTITION BY table_name, column_name) to count how many rows have the same table and column name and then WHERE ... &gt;= 2. Also:


I don't think you need the DISTINCT.
If you do need the DISTINCT there, it can be with either one more CTE to apply it or use GROUP BY as below.
Please don't use single quotes for table and column aliases. If you need to, use square brackets or double quotes.


The query:

  ;
WITH cte AS
(
    SELECT   
        table_name               AS Tabela, 
        column_name              AS Coluna,
        character_maximum_length AS Tamanho, 
        numeric_precision        AS Precisao_Numerica, 
        numeric_scale            AS Escala_Numerica,
        COUNT(*) OVER (PARTITION BY table_name, column_name) 
                                 AS Count_Duplicates
    FROM TBL_RELATORIO_TAMANHOS
    GROUP BY 
        table_name, column_name,  character_maximum_length,
        numeric_precision, numeric_scale
)
SELECT 
    cte.*,
    ROW_NUMBER() OVER (PARTITION BY Tabela, Coluna) AS Rn
FROM cte
WHERE Count_Duplicates &gt;= 2
ORDER BY Tabela, Coluna, Rn 
  ;