【问题标题】:Combine Multiple Columns in Single field在单个字段中合并多个列
【发布时间】:2014-07-09 19:37:59
【问题描述】:

![焊接跟踪数据库][1]

大家好!

我希望你能帮助我解决我的问题。 我有 6 列(Col1、Col2、Col3、Col4、Col5、Col6),每一行都有一个唯一的 ID。 现在,我尝试在一个字段中合并/组合 6 列,但我坚持使用 + 和 & 操作。我希望查询结果如下。

第 1 行(Col1=10 Col2=10,Col3=10,Col4=10,Col5=10,Col6=10;查询=10)
第 2 行(Col1=10 Col2=10,Col3=20,Col4=20,Col5=30,Col6=30;查询=10/20/30)
第 3 行(Col1=10 Col2=20, Col3=30, Col4=40, Col5=50, Col6=60;查询=10/20/30/40/50/60)

提前谢谢你们!

【问题讨论】:

    标签: ms-access


    【解决方案1】:

    首先我想说我认为您应该重新审视您存储数据的方式。如果您觉得需要以这种方式按列分组,则可能是没有“正确”设置。通常情况下,这是您存储数据的方式的问题。我可能弄错了,但根据给定的信息感觉就是这样。

    为了解决您的实际问题,我将使用自定义函数。我想不出只使用 SQL 来查询和按列分组的方法

    在模块中抛出函数并在查询中调用它(例如CombinedColumns: GroupByColumns([a],[b],[c],[d],[e],[f])),其中a,b,c,d,e,f 是字段名称

    Function GroupByColumns(ParamArray flds())
        Dim dict As Dictionary
        Dim key
        Dim i As Long
    
        Set dict = New Dictionary
        For i = LBound(flds) To UBound(flds)
            If dict.Exists(flds(i)) = False Then
                dict.Add flds(i), flds(i)
            End If
        Next
        For Each key In dict.Keys
            If GroupByColumns = "" Then
                GroupByColumns = key
            Else
                GroupByColumns = GroupByColumns & "/" & key
            End If
        Next
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多