将 Access 表中的多个列连接到一个特定行

Posted

技术标签:

【中文标题】将 Access 表中的多个列连接到一个特定行【英文标题】:Concatenating Multiple columns in an Access table to one specific row 【发布时间】:2015-04-09 17:04:40 【问题描述】:

我一直在网上寻找答案,但我找不到有效的答案(我确定这是我做错的事情)

我在 Access 中有一个名为 TBL003_Combined Data 的表,其中包含以下列: 已上传、参考编号、数量、零件编号、项目描述、运送至 4/8/2015, 123, 20, 9125xtr, 样品项目, XYZ 公司, 789 Address Lane, Somewhere,US 159632 4/8/2015, 123, 16, 22578xtz, 样品项目 2, XYZ 公司, 789 Address Lane, Somewhere,US 159632 4/8/2015, 123, 8, 7758rty, sample item3, XYZ Company, 789 Address Lane, Somewhere,US 159632

我想做的是对于每个唯一的 [REF ID],我想将 QTY、[PART NUMBER]、[ITEM DESCRIPTION] 和 [SHIP TO] 连接到一个名为 [CS_ITEM DESCRIPTION] 的新列。

我在 Allen Brown 的网站上尝试了以下内容:

Public Function ConcatRelated(strField As String, _
    strTable As String, _
    Optional strWhere As String, _
    Optional strOrderBy As String, _
    Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
    'Purpose:   Generate a concatenated string of related records.
    'Return:    String variant, or Null if no matches.
    'Arguments: strField = name of field to get results from and concatenate.
    '           strTable = name of a table or query.
    '           strWhere = WHERE clause to choose the right values.
    '           strOrderBy = ORDER BY clause, for sorting the values.
    '           strSeparator = characters to use between the concatenated values.
    'Notes:     1. Use square brackets around field/table names with spaces or odd characters.
    '           2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
    '           3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
    '           4. Returning more than 255 characters to a recordset triggers this Access bug:
    '               http://allenbrowne.com/bug-16.html
    Dim rs As DAO.Recordset         'Related records
    Dim rsMV As DAO.Recordset       'Multi-valued field recordset
    Dim strSQL As String            'SQL statement
    Dim strOut As String            'Output string to concatenate to.
    Dim lngLen As Long              'Length of string.
    Dim bIsMultiValue As Boolean    'Flag if strField is a multi-valued field.

    'Initialize to Null
    ConcatRelated = Null

    'Build SQL string, and get the records.
    strSQL = "SELECT DISTINCT " & strField & " FROM " & strTable
    If strWhere <> vbNullString Then
        strSQL = strSQL & " WHERE " & strWhere
    End If
    If strOrderBy <> vbNullString Then
        strSQL = strSQL & " ORDER BY " & strOrderBy
    End If
    Set rs = DBEngine(0)(0).OpenRecordset(strSQL, dbOpenDynaset)
    'Determine if the requested field is multi-valued (Type is above 100.)
    bIsMultiValue = (rs(0).Type > 100)

    'Loop through the matching records
    Do While Not rs.EOF
        If bIsMultiValue Then
            'For multi-valued field, loop through the values
            Set rsMV = rs(0).Value
            Do While Not rsMV.EOF
                If Not IsNull(rsMV(0)) Then
                    strOut = strOut & rsMV(0) & strSeparator
                End If
                rsMV.MoveNext
            Loop
            Set rsMV = Nothing
        ElseIf Not IsNull(rs(0)) Then
            strOut = strOut & rs(0) & strSeparator
        End If
        rs.MoveNext
    Loop
    rs.Close

    'Return the string without the trailing separator.
    lngLen = Len(strOut) - Len(strSeparator)
    If lngLen > 0 Then
        ConcatRelated = Left(strOut, lngLen)
    End If

Exit_Handler:
    'Clean up
    Set rsMV = Nothing
    Set rs = Nothing
    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
    Resume Exit_Handler
End Function

我的查询使用了以下内容:

SELECT DISTINCT
    [REF ID],
    ConcatRelated("[QTY],[PART NUMBER], [ITEM DESCRIPTION], [SHIP TO]",
        "[TBL003_Combined Data]",
        "[REF ID] = " & [REF ID],
        "("[QTY],[PART NUMBER], [ITEM DESCRIPTION], [SHIP TO]",
        "/"
        ) AS CS_ITEM DESCRIPTIONS
FROM [TBL003_Combined Data];

我不断收到以下错误:

“查询表达式中的语法错误(缺少运算符)......”

然后它会列出我在上面引用的整个 SQL(选择 Distinct)。任何帮助我指出正确的方向或建议我做错的原因(因为我确定是我)将不胜感激。

补充:我试图达到的结果是数据如下:

已上传,参考 ID,CS_ITEM 描述 4/8/2015, 123, 20 / 9125xtr / sample item / 16 / 22578xtz /sample item2 / 8 / 7758rty / sample item3 / XYZ Company, 789 Address Lane, Somewhere,US 159632

【问题讨论】:

【参考方案1】:

试试这个:

SELECT distinct [REF ID],
[QTY] + ' ' + [PART NUMBER] + ' ' + [ITEM DESCRIPTION] + ' ' + [SHIP TO] 
AS 'CS_ITEM DESCRIPTIONS' 
FROM [TBL003_Combined Data];

【讨论】:

谢谢 Jeremy W,但它仍然给我一个语法错误。另外,我认为 (+) 的行为类似于 (& " ",) 字符串,因为它将加入每一行,但我需要将整列组合成一行。我一直在绞尽脑汁,所以任何其他建议将不胜感激:) 如果你的意思不是串联,“列合并为一行”是什么意思? 抱歉给您带来了困惑。我确实想将 4 个不同列中的项目连接到一个字段(行)中(如上面的示例数据)。 “+”不起作用,仍然是语法错误,我认为“+”就像“&”,我试过了,但它只连接每行,我需要将整个列连接在一个单元格中(排)。这甚至可能吗?目前,我在 Excel 中执行此操作,然后将数据导入数据库,得到我的结果,也许这是我实现目标的唯一方法。任何可以帮助我在 Access 中执行此操作的建议将不胜感激。

以上是关于将 Access 表中的多个列连接到一个特定行的主要内容,如果未能解决你的问题,请参考以下文章

将两个数据库列连接到一个结果集列

将一个表的两列连接到另一个表的一列

如何在 spark DataFrame 中将多个浮点列连接到一个 ArrayType(FloatType()) 中?

在单个查询中将一个表的多个列连接到另一个表的单个列

使用 Critera 或 QueryOver API 将一列连接到多个表

使用 vba/macros 将多个 CSV 文件中的特定列导入 Access 2013