【问题标题】:How to Remove the duplicates in SQL Table and concat other part [duplicate]如何删除 SQL 表中的重复项并连接其他部分 [重复]
【发布时间】:2015-11-02 10:47:20
【问题描述】:

假设我有代表员工信息的 DataTable /SQL Table(MicroSoft SQL)

其中包含名字、姓氏、年龄、公司、工作年限、学位

我想根据名字、姓氏、年龄组合信息

company,yearsexperience,Degree 必须连接到对应的单元格中

firstname   lastname   age   company   yearsofexperience      Degree

john         muller     21    IBM           4years            MBA   
jan          tonny      22,   MSoft         1years            MS
martin       tata       21    apple         2years            PHD
john         Muller     21    sony          3years            MBA
james        muller     21    IBM           4years            PHD   
jan          tonny      22    Telsa         1years            BS     
martin       tata       21    sun           2years            MBA
james        Muller     21    TCS           3years            BS

注意:MS SQL解决方案不是Mysql

请找到我删除重复行并在特定列中连接其他数据的方法

例如,在上面的示例中,我想结合其他 3 个类似条目中的信息

firstname   lastname   age   company            yearsofexperience      Degree

john      muller        21   IBM,sony,              4years,3years,        MBA,MBA   
jan       tonny         22,  MSoft,Telsa            1years,1years         MS,BS
martin    tata          21   apple,sun              2years,2years         PHD,MBA
james     muller        21   IBM,TCS               4years,3years          PHD,BS

是的,我正在寻找实现此功能的最佳方法

如果我将表拆分为 2 个不同的表,这是一种好方法吗?可能基于主键匹配。我们可以合并其他条目吗?

请帮助我提前谢谢(+1)

【问题讨论】:

  • 请不要标记未涉及的产品。 (你在这里同时使用 MySQL 和 MS SQL Server 吗?)

标签: sql-server-2008 sql-server-2005


【解决方案1】:

如果您使用的是 SQL Server,请尝试以下操作:

SELECT T1.firstname
,t1.lastname
,t1.age
,Company = SubString (( SELECT ', ' + T2.company 
FROM table_name as T2 
WHERE T1.firstname = T2.firstname
and t1.lastname = t2.lastname
and t1.age = t2.age 
FOR XML PATH ( '' ) ), 3, 1000) 

,yearsofexperience = SubString (( SELECT ', ' + T2.yearsofexperience 
FROM table_name as T2 
WHERE T1.firstname = T2.firstname
and t1.lastname = t2.lastname
and t1.age = t2.age 
FOR XML PATH ( '' ) ), 3, 1000) 

,yearsofexperience = SubString (( SELECT ', ' + T2.Degree 
FROM table_name as T2 
WHERE T1.firstname = T2.firstname
and t1.lastname = t2.lastname
and t1.age = t2.age 
FOR XML PATH ( '' ) ), 3, 1000) 

FROM table_name as T1 
GROUP BY T1.firstname, t1.lastname ,t1.age

参考:Create A Comma Delimited List From a Column in SQL Server

【讨论】:

    【解决方案2】:

    你可以试试group bygroup_concat的SQL

    SELECT *,group_concat(company) as company,
    group_concat(yearsofexperience) as yearsofexperience,
    group_concat(Degree) as Degree  
    from table group by firstname
    group by lastname
    group by age
    

    更多请点击链接 Merge data in two row into one

    【讨论】:

    • 太糟糕的 OP 使用 SQL Server。
    • OP 使用的是 SQL Server 而不是 Mysql。
    • OP 使用的是 MS SQL Server,它不支持 MySQL 特有的GROUP_CONCAT()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2013-01-14
    • 2012-07-05
    • 2021-07-01
    • 2014-07-21
    相关资源
    最近更新 更多