首页 文章

在单个字段中组合多个列

提问于
浏览
0

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

嗨,大家好!

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

Row1(Col1 = 10 Col2 = 10,Col3 = 10,Col4 = 10,Col5 = 10,Col6 = 10;查询= 10)
Row2(Col1 = 10 Col2 = 10,Col3 = 20,Col4 = 20,Col5 = 30,Col6 = 30;查询= 10/20/30)
Row3(Col1 = 10 Col2 = 20,Col3 = 30,Col4 = 40,Col5 = 50,Col6 = 60;查询= 10/20/30/40/50/60)

提前谢谢你们!

1 回答

  • 0

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

    为了解决你的实际问题,我会使用自定义功能 . 我想不出只使用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
    

相关问题