从数据表中删除重复并使值vb.net

时间:2014-02-07 07:25:55

标签: vb.net

我有一个数据表有两列和名称列中的重复记录,想要删除重复列而不是重复列的值,需要附加“,”

Name    Value
Arun    x,c
Kumar   j,k
Jai     a,b
Balu    a,c
Kumar   j,m
Arun    c,x,w

我希望输出为

Name    Value
Arun    x,c,w
Kumar   j,k,m
Jai     a,b
Balu    a,c

2 个答案:

答案 0 :(得分:1)

试试这个

SELECT Name,Value + ',' + value  
FROM Table1
Group By Name,Value

答案 1 :(得分:0)

你可以尝试这个(关于如何工作的想法在评论中说明):

Dim dt As New DataTable
'source DataTable has 2 columns : Name and Value
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Value", GetType(String))
'add 2 rows to source DataTable
dt.Rows.Add("Arun", "x,c")
dt.Rows.Add("Arun", "c,x,w")

'group rows by Name
'for each group join value by comma so "x,c" + "c,x,w" become "x,c,c,x,w"
'then split by comma and call distinct to remove duplicate chars. result: x|c|w
'join them again by comma. result: "x,c,w"
Dim query = (From row In dt
            Group row By name = row.Field(Of String)("Name") Into nameGroup = Group
            Select New With {
                .Name = name,
                .Value = String.Join(",", String.Join(",", nameGroup.Select(Function(x) x.Field(Of String)("Value")).ToList()).Split(",").Distinct())
                }
            ).ToList()

Dim dt2 As New DataTable
dt2.Columns.Add("Name", GetType(String))
dt2.Columns.Add("Value", GetType(String))
'add distinct data to result DataTable
For i As Integer = 0 To query.Count
    dt2.Rows.Add(query(i).Name, query(i).Value)
Next