简体   繁体   English

在 Microsoft Access 中使用 SQL 将多值字段拆分为不同的行

[英]Split multi-value field into different rows using SQL in Microsoft Access

I've got a simple table in Microsoft Access that looks like this:我在 Microsoft Access 中有一个简单的表格,如下所示:

Primary Key首要的关键 Applications List应用列表
123 123 <Value>|<Value>,<Value>|<Value>
456 456 <Value>|<Value>,<Value>|<Value>

I need to break out the list of applications into separate rows using the “,” as a delimiter so the end result is a table that looks like this:我需要使用“,”作为分隔符将应用程序列表分成单独的行,因此最终结果是一个如下所示的表格:

Primary Key首要的关键 Applications List应用列表
123 123 <Value>|<Value>
123 123 <Value>|<Value>
456 456 <Value>|<Value>
456 456 <Value>|<Value>

I've tried using the Split function but can't figure out how to split on the “,” and output the results to a different row like the second table above.我试过使用 Split function 但无法弄清楚如何在“,”和 output 上将结果拆分到不同的行,如上面的第二个表。 I would greatly appreciate your help figuring this one out.非常感谢您帮助解决这个问题。 Thanks so much!非常感谢!

If you can put a limit on the length of Application List you can build a number table如果你可以限制应用程序列表的长度,你可以建立一个数字表

CREATE TABLE NumTable (ID integer)

up-front with as many rows as there are characters in the longest Application List .最前面的行数与最长的应用程序列表中的字符数一样多。 Assuming that the longest [Application List] is 1000 characters long, ID=1, ID=2, ..., ID=1000), and then use something like this:假设最长的[Application List]是1000个字符长,ID=1,ID=2, ...,ID=1000),然后使用这样的东西:

select T.[Primary Key], 
       mid(T.AL,NT.ID+1
           , iif(instr(NT.ID+1,t.AL,',')>0
           ,     instr(NT.ID+1,t.AL,',')-1-nt.ID,1000)) as 
from (select [Primary Key], ',' & [Application List] as AL from tblYourTable) as T
     inner join
     NumTable as NT
     on mid(t.AL,NT.ID,1)=','

You can build the number table in EXCEL and paste it;可以在EXCEL中建好号码表,然后粘贴; or write a little VBA routine, or even create it dynamically (*).或编写一些 VBA 例程,甚至动态创建它 (*)。

I can't imagine it performing very well if the volume is high.如果音量很高,我无法想象它会表现得很好。

Let us know how you proceed!让我们知道您是如何进行的!

(*) Generate numbers 1 to 1000000 in MS Access using SQL (*) 在 MS Access 中使用 SQL 生成数字 1 到 1000000

If you setup a Table1 and a Table2 with the same field names, you can run this function to do it for you - it basically loops through Table1 records, splits the Applications List into multiple fields and then inserts new records for each field.如果您设置具有相同字段名称的 Table1 和 Table2,您可以运行此 function 来为您完成它 - 它基本上遍历 Table1 记录,将应用程序列表拆分为多个字段,然后为每个字段插入新记录。

Public Sub SplitDataIntoNewTable()

    Const DATA_SEPARATOR    As String = ","
    Dim qdf                 As DAO.QueryDef
    Dim rsOld               As DAO.Recordset
    Dim rsNew               As DAO.Recordset
    
    Dim i                   As Integer
    Dim lngNumAdded         As Long
    
    Dim lngKey              As Long
    Dim strList             As String
    Dim strNewList          As String
    
    Dim varLists            As Variant
        
    Set rsOld = CurrentDb.OpenRecordset("Table1", dbOpenDynaset, dbReadOnly)
    Set rsNew = CurrentDb.OpenRecordset("Table2", dbOpenDynaset)
    
    With rsOld
        While Not .EOF
            lngKey = ![Primary Key]
            strList = ![Applications List]
            varLists = Split(strList, DATA_SEPARATOR)
            With rsNew
                For i = LBound(varLists) To UBound(varLists)
                    ' Add new record for every list split out of old data
                    .AddNew
                    ' Note that this CANNOT actually be defined as a PRIMARY KEY - it will have duplicates
                    ![Primary Key] = lngKey
                    ![Applications List] = varLists(i)
                    lngNumAdded = lngNumAdded + 1
                    .Update
                Next i
            End With
            .MoveNext
        Wend
        
        rsNew.Close
        .Close
    End With
    
    MsgBox "Added " & lngNumAdded & " New Records"
    
    Set rsOld = Nothing
    Set rsNew = Nothing
End Sub

For example I had Table1 look like this:例如,我让 Table1 看起来像这样:

表格1

And resulting Table2 ended up like this结果 Table2 变成了这样

表 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM