访问具有相同ID但在其他2列中具有不同值的行

时间:2014-04-10 07:24:43

标签: sql ms-access

我在Access 2007中有以下表格:

Route   Cust    Planners
4401    1004    jasper
4401    1005    onno
4401    1006    jasper
4402    1007    bart
4402    1008    marcel
4402    1059    onno
4403    1124    bart
4403    1165    marcel
4403    1198    marcel
4403    1201    mustafa
4403    1225    bart
4401    1178    maurice

我想要这个结果(所有的人和所有规划者一起拥有相同的路线):

Route   Cust                      Planners
4401    1004;1006;1178;1005       jasper;maurice;onno
4402    1007;1008;1059            bart;marcel;onno
4403    1124;1225;1165;1198;1201  mustafa;marcel;bart 

这可能与查询有关,还是仅通过VBA代码? 提前谢谢!

编辑@Dan(求助): 谢谢你的步骤,我有想法做这样的事情,但不知道具体如何。 下面的代码对我有用(它可以更简单,更少的代码,但它完成工作)

Dim rs As DAO.Recordset
Dim cust, planners As String
Dim route, route2, cntr As Integer

DoCmd.RunSQL "Delete * FROM details2"

Set rs = CurrentDb.OpenRecordset("select * from details order by route, cust")

rs.MoveFirst
cntr = 0
Do While Not rs.EOF
    route = rs.Fields("Route")
    'If route readed from recordset is not the same as local variable then insert that route and give local variable the new route and clear cust and planners
    If route <> route2 Then
        'If going through loop for first time => do no insert
        If cntr > 0 Then
            'Delete last comma for both cust and planners field and insert that route
            cust = Left(cust, Len(cust) - 1)
            planners = Left(planners, Len(planners) - 1)
            DoCmd.RunSQL "insert into details2 (Route, Cust, Planners) Values (" & route2 & ", '" & cust & "', '" & planners & "')"
        End If
        route2 = route
        cust = ""
        planners = ""
    End If

    'If route readed is the same as local variable, add cust and planner to the string but only if it isn't in the string already
    If route = route2 Then
        If InStr(cust, CStr(rs.Fields("cust"))) = 0 Then cust = cust + CStr(rs.Fields("cust")) & ","
        If InStr(planners, rs.Fields("planners")) = 0 Then planners = planners + rs.Fields("planners") & ","
    End If
    rs.MoveNext
    cntr = cntr + 1
Loop

'Delete last comma for cust and planners field and add last route
cust = Left(cust, Len(cust) - 1)
planners = Left(planners, Len(planners) - 1)
DoCmd.RunSQL "insert into details2 (Route, Cust, Planners) Values (" & route2 & ", '" & cust & "', '" & planners & "')"

这给了我以下结果:

Route   Cust                        Planners
4401    1004,1005,1006,1178         jasper,onno,maurice
4402    1007,1008,1059              bart,marcel,onno
4403    1124,1165,1198,1201,1225    bart,marcel,mustafa

非常感谢!

2 个答案:

答案 0 :(得分:0)

虽然Access SQL本身没有任何方法可以将字符串连接作为聚合组来执行,但您可以使用SQL和VBA的组合来实现此目的。

我不能给你实际的代码,但这个想法是按照以下几行:

  1. 创建一个新表以保存输出。确保您的Cust和Planners列足够长。
  2. 在VBA中,创建一个变量来存储Route-number。将其初始化为0或其他未出现在数据库中的值。还为Cust和Planners创建字符串变量。将它们初始化为空字符串。
  3. 打开按路由排序的记录集,并iterate through all records
  4. 对于每条记录,检查Route列的值是否等于Route-variable中存储的值。
  5. 如果值相同,请将Cust和Planners列的值添加(连接)到它们各自的变量。
  6. 如果Route列值与Route变量值不同,insert a new record in your output table与变量的当前值相同。重置Cust和Planners变量,并从当前记录中分配值。将新的Route列值分配给Route变量。
  7. 继续迭代。
  8. 迭代完成后,在输出表中执行最后一次插入。输出表现在应该保存您正在查找的结果。

答案 1 :(得分:0)

您必须创建VBA功能。然后使用以下查询。

<强> Here is the Source link

SELECT CompanyName,  ConcatRelated("Cust", "Table_name", "Route= " & [Route]) 
FROM Table_name;