请教能否把access2003的一个表的两条记录中的某个字段合并起来,只显示一条记录
请问能否把access2003的一个表的两条记录中的某个字段合并起来,只显示一条记录啊
如:姓名 性别 职务 联系电话
李三 男 销售经理 138xxxxxxxx
王四 女 客服经理 152xxxxxxxx
李三 男 客服经理 138xxxxxxxx
合并后:
姓名 性别 职务 联系电话
李三 男 销售经理/客服经理 138xxxxxxxx
王四 女 客服经理 152xxxxxxxx
------最佳解决方案--------------------
只能 在ACCESS下运行
select 姓名,max(dc(姓名)) from tt group by 姓名
模块:
Function dc(ByVal dd As string) As String
f1 = ""
Set rs = CurrentDb.OpenRecordset("select 职务 from tt where 姓名='" & dd & "'")
Do While Not rs.EOF
f1 = f1 & rs(0) & "+"
rs.MoveNext
Loop
dc = Left(f1, Len(f1) - 1)
End Function
------其他解决方案--------------------
select 姓名,max(dc(姓名)) into newtt from tt group by 姓名
删除旧表
------其他解决方案--------------------
自定义函数:
select distinct 姓名, 性别, concateColumn("tName","姓名","职务",姓名,"/") as N职务 ,联系电话 from tName
Public Function concateColumn(sTable As String, sRow As String, sCol As String, vRow As String, Optional delimiter As String = "")
'合并(列转行) sTable 引用的表名; sRow 分组的字段名; sCol 列转行的字段名; vRow 分组的值; Optional 分隔符
Dim rs As New ADODB.Recordset
Dim sSQL As String
Dim sResult As String
Dim i As Integer
sResult = ""
sSQL = "select distinct " & sCol & " from " & sTable & " where " & sRow & "='" & vRow & "'"
rs.Open sSQL, CurrentProject.Connection, 1, 1
For i = 1 To rs.RecordCount
If i = rs.RecordCount Then
sResult = sResult & rs.Fields(0).Value
Else
sResult = sResult & rs.Fields(0).Value & delimiter
End If
rs.MoveNext
Next i
rs.Close
Set rs = Nothing
concateColumn = sResult
End Function
------其他解决方案--------------------
如:姓名 性别 职务 联系电话
李三 男 销售经理 138xxxxxxxx
王四 女 客服经理 152xxxxxxxx
李三 男 客服经理 138xxxxxxxx
合并后:
姓名 性别 职务 联系电话
李三 男 销售经理/客服经理 138xxxxxxxx
王四 女 客服经理 152xxxxxxxx
------最佳解决方案--------------------
只能 在ACCESS下运行
select 姓名,max(dc(姓名)) from tt group by 姓名
模块:
Function dc(ByVal dd As string) As String
f1 = ""
Set rs = CurrentDb.OpenRecordset("select 职务 from tt where 姓名='" & dd & "'")
Do While Not rs.EOF
f1 = f1 & rs(0) & "+"
rs.MoveNext
Loop
dc = Left(f1, Len(f1) - 1)
End Function
------其他解决方案--------------------
select 姓名,max(dc(姓名)) into newtt from tt group by 姓名
删除旧表
------其他解决方案--------------------
自定义函数:
select distinct 姓名, 性别, concateColumn("tName","姓名","职务",姓名,"/") as N职务 ,联系电话 from tName
Public Function concateColumn(sTable As String, sRow As String, sCol As String, vRow As String, Optional delimiter As String = "")
'合并(列转行) sTable 引用的表名; sRow 分组的字段名; sCol 列转行的字段名; vRow 分组的值; Optional 分隔符
Dim rs As New ADODB.Recordset
Dim sSQL As String
Dim sResult As String
Dim i As Integer
sResult = ""
sSQL = "select distinct " & sCol & " from " & sTable & " where " & sRow & "='" & vRow & "'"
rs.Open sSQL, CurrentProject.Connection, 1, 1
For i = 1 To rs.RecordCount
If i = rs.RecordCount Then
sResult = sResult & rs.Fields(0).Value
Else
sResult = sResult & rs.Fields(0).Value & delimiter
End If
rs.MoveNext
Next i
rs.Close
Set rs = Nothing
concateColumn = sResult
End Function
------其他解决方案--------------------