SQL 删除重复记录 [重复]

Posted

技术标签:

【中文标题】SQL 删除重复记录 [重复]【英文标题】:SQL Remove Duplicate Records [duplicate] 【发布时间】:2016-01-07 06:01:08 【问题描述】:

我有以下疑问:

SELECT
   CAST(t. NAME AS CHAR(12)) AS [ TABLE ], 
   CAST(c. NAME AS CHAR(20)) AS [ COLUMN ],
   CAST(d. NAME AS CHAR(9)) AS [ DATA type ], 
   CASE WHEN d. NAME IN ('char', 'varchar') THEN
     STR (c.max_length, 6, 0)
   ELSE
     ''
   END AS [ Length ],
   CASE WHEN d. NAME IN ('numeric', 'decimal') THEN
     STR (c. PRECISION, 9, 0)
   ELSE
     ''
   END AS [ PRECISION ],
   CASE WHEN d. NAME IN ('numeric', 'decimal') THEN
     STR (c.scale, 5, 0)
   ELSE
     ''
   END AS [ Scale ],
   CASE c.is_nullable
     WHEN 0 THEN
       'not null'
     ELSE
       ''
     END AS [ Nullable ]
FROM
   sys. COLUMNS AS c
JOIN sys. TABLES AS t ON c.object_id = t.object_id
JOIN sys.types AS d ON c.system_type_id = d.system_type_id
ORDER BY
   1,
   column_id;

这给了我这样的东西:

    Table   | Column       | Data type|
    __________________________________
    Customer|  CustomerCode|  int     |  
    Customer| LastName     | varchar  | 
    Customer|  FirstName   |  varchar | 
    Customer|  AreaCode    |  char    |
    Employee|  Title       |  char    |
    Employee|  LastName    |  varchar |
    Employee|  FirstName   |  varchar |

但是,我只想在 Table 中显示重复的单词一次,如下所示:

    Table   | Column       | Data type|
    __________________________________
    Customer|  CustomerCode|  int     |  
            | LastName     | varchar  | 
            |  FirstName   |  varchar | 
            |  AreaCode    |  char    |
    Employee|  Title       |  char    |
            |  LastName    |  varchar |
            |  FirstName   |  varchar |

我已经尝试了几个小时,但我似乎无法做到正确。请帮忙!!

【问题讨论】:

【参考方案1】:

SQL-Server 中你可以这样做:

select case when rn > 1 then '' else [Table] end as [Table], 
       [column],
       [data type]
from(
    select  [column], 
            [type], 
            row_number() over(partition by [Table] order by [Table]) rn
    from YourTable
) x

您提供的样本将类似于:

SELECT CASE WHEN rn > 1 THEN '' ELSE [TABLE] END AS [TABLE], 
       [COLUMN], [DATA type], [Length], [PRECISION], [Scale], [Nullable]
FROM(
    SELECT
       CAST(t. NAME AS CHAR(12)) AS [TABLE], 
       CAST(c. NAME AS CHAR(20)) AS [COLUMN], CAST(d. NAME AS CHAR(9)) AS [DATA type], 
    CASE WHEN d. NAME IN ('char', 'varchar') THEN
       STR (c.max_length, 6, 0)
    ELSE
       ''
    END AS [Length], CASE
    WHEN d. NAME IN ('numeric', 'decimal') THEN
       STR (c. PRECISION, 9, 0)
    ELSE
       ''
    END AS [PRECISION], CASE
    WHEN d. NAME IN ('numeric', 'decimal') THEN
       STR (c.scale, 5, 0)
    ELSE
       ''
    END AS [Scale], CASE c.is_nullable
    WHEN 0 THEN
       'not null'
    ELSE
       ''
    END AS [Nullable],
    ROW_NUMBER() OVER(PARTITION BY CAST(t. NAME AS CHAR(12)) ORDER BY CAST(t. NAME AS CHAR(12))) rn,
    [column_id]
    FROM
       sys. COLUMNS AS c
    JOIN sys. TABLES AS t ON c.object_id = t.object_id
    JOIN sys.types AS d ON c.system_type_id = d.system_type_id
)x

【讨论】:

非常感谢!!终于成功了!! @jungkookie 他给了你很棒的解释为什么你还在问? @Jonathan 我问,在看到他编辑的答案之前,我现在理解得更好了:) 上一条评论在哪里??被删除了天哪!

以上是关于SQL 删除重复记录 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

sql中如何删除一个表中重复的记录?

sql查询重复记录删除重复记录方法大全

sql查询重复记录删除重复记录方法大全

sql查询重复记录删除重复记录方法大全

SQL 删除重复记录 [重复]

Oracle 查询并删除重复记录的SQL语句