如何删除 SQL Table 中的重复项并连接其他部分
How to Remove the duplicates in SQL Table and concat other part
假设我有代表员工信息的数据Table /SQL Table(Microsoft SQL)
其中包含名字、姓氏、年龄、公司、工作年限、学位
我想根据名字、姓氏、年龄合并信息
company,yearsofexperience,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
注:MSSQL解不了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
好的,我正在寻找实现这个的最佳方法
如果我将 Table 拆分为 2 个不同的表,这是个好方法吗?可能基于主键匹配。我们可以合并其他条目吗?
请帮我提前谢谢(+1)
您可以尝试 group by
和 group_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
更多请关注link
Merge data in two row into one
如果您使用的是 SQL 服务器,那么试试这个:
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
假设我有代表员工信息的数据Table /SQL Table(Microsoft SQL)
其中包含名字、姓氏、年龄、公司、工作年限、学位
我想根据名字、姓氏、年龄合并信息
company,yearsofexperience,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
注:MSSQL解不了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
好的,我正在寻找实现这个的最佳方法
如果我将 Table 拆分为 2 个不同的表,这是个好方法吗?可能基于主键匹配。我们可以合并其他条目吗?
请帮我提前谢谢(+1)
您可以尝试 group by
和 group_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
更多请关注link Merge data in two row into one
如果您使用的是 SQL 服务器,那么试试这个:
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