MS-Access VBA SQL,如果子查询条件,效率和改进

时间:2018-04-22 02:45:45

标签: sql ms-access access-vba ms-access-2016

我设法让我的代码工作,但执行需要很长时间。我使用一系列open和close记录集语句来获取我在执行最终SQL语句并将行写入表之前查找的addr_ImportID。

有没有更好的方法来做我尝试使用更好的子查询,案例陈述,如果条件或其他方式?我对子查询的使用减慢了速度,因此在不打开和关闭记录集的情况下操作数据会更有效。还是一系列更好的选择陈述?

以下是表格的样本:

CnBio_ID | Addr_Type | Addr_ImportID | CnAdrAll_1_IDKEY | CnAdrAll_1_DateLastChanged
0001009  | Previous  | 12344         | 1                | 03/15/2014
0001009  | Home      | H2345         | 3                | 01/02/2017
0001009  | Home      | A2344         | 2                | 01/02/2017
1223144  | Previous  | 23441         | 7                | 05/23/2010
1223144  | Other     | 31333         | 6                | 08/21/2016
1223144  | Previous  | 45848         | 5                | 08/21/2016

下面的代码段是我代码的一部分。

strSql2 = "SELECT Addr_ImportID From [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & rs(0) & "' " & _
        "AND CnAdrAll_1_DateLastChanged IN (SELECT Max(CnAdrAll_1_DateLastChanged) FROM [Non-Preferred Home Addresses] " & _
        "Where CnBio_ID = '" & rs(0) & "' AND Addr_ImportID IN (SELECT Addr_ImportID From [Non-Preferred Home Addresses] " & _
        "WHERE CnBio_ID = '" & rs(0) & "' AND Addr_Type = 'Home'));"
        Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)
        If Not (rsMaxAddressImportID.EOF) Then
            'Condition where more than one Home address exists updated on the same date
            If (rsMaxAddressImportID.RecordCount > 1) Then
                rsMaxAddressImportID.Close
                strSql2 = "SELECT Addr_ImportID FROM [Non-Preferred Home Addresses] WHERE CnAdrAll_1_IDKEY = " & _
                "(SELECT Max(CnAdrAll_1_IDKEY) FROM [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & rs(0) & "' AND Addr_Type = 'Home');"
                Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)
                MsgBox "Row = " & i
            End If
  • For Each CnBio_ID
  • 如果存在Home地址,请选择Max DateLastChanged行的Addr_ImportID
    • 如果多个家庭住址在同一天上次更新,请选择带有Max CnAdrAll_1_IDKEY的行的Addr_ImportID
  • 如果没有家庭住址,请选择Max DateLastChanged的行的Addr_ImportID
    • 如果多个地址最后一次在同一天更新,请选择最大CnAdrAll_1_IDKEY行的Addr_ImportID

Parfait提问后更新

'Select Distinct CIDs to loop through
'strSql = "SELECT CnBio_ID FROM [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & ConstID & "'Group By CnBio_ID;"
strSql = "SELECT CnBio_ID FROM [Non-Preferred Home Addresses] Group By CnBio_ID;"
Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)

If Not (rs.EOF And rs.BOF) Then
    'MsgBox rs.RecordCount

    'This section will loop through all distinct CIDs.
    i = 1
    Do Until rs.EOF

        'Select Non-Preferred Home Address with latest date updated
        strSql2 = "SELECT Addr_ImportID From [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & rs(0) & "' " & _
        "AND CnAdrAll_1_DateLastChanged IN (SELECT Max(CnAdrAll_1_DateLastChanged) FROM [Non-Preferred Home Addresses] " & _
        "Where CnBio_ID = '" & rs(0) & "' AND Addr_ImportID IN (SELECT Addr_ImportID From [Non-Preferred Home Addresses] " & _
        "WHERE CnBio_ID = '" & rs(0) & "' AND Addr_Type = 'Home'));"
        Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)
        If Not (rsMaxAddressImportID.EOF) Then
            'Condition where more than one Home address exists updated on the same date
            If (rsMaxAddressImportID.RecordCount > 1) Then
                rsMaxAddressImportID.Close
                strSql2 = "SELECT Addr_ImportID FROM [Non-Preferred Home Addresses] WHERE CnAdrAll_1_IDKEY = " & _
                "(SELECT Max(CnAdrAll_1_IDKEY) FROM [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & rs(0) & "' AND Addr_Type = 'Home');"
                Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)
                MsgBox "Row = " & i
            End If

            'rsMaxAddressImportID.Close
        Else
            'Condition if all non-preferred addresses are not Home
            rsMaxAddressImportID.Close

            'Select max date last updated for next select statement
            maxDateSql = "SELECT Max(CnAdrAll_1_DateLastChanged) " & _
            "FROM [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & rs(0) & "';"
            Set rs2 = DBEngine(0)(0).OpenRecordset(maxDateSql, dbOpenDynaset)

            strSql2 = "SELECT Addr_ImportID FROM [Non-Preferred Home Addresses] " & _
            "WHERE [Non-Preferred Home Addresses].CnBio_ID = '" & rs(0) & "' " & _
            "AND [Non-Preferred Home Addresses].CnAdrAll_1_DateLastChanged = #" & rs2(0) & "#;"
            Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)
            'MsgBox rsMaxAddressImportID(0)
            If Not (rsMaxAddressImportID.EOF) Then
                'MsgBox rsMaxAddressImportID.RecordCount
                If (rsMaxAddressImportID.RecordCount > 1) Then
                    rsMaxAddressImportID.Close
                    strSql2 = "SELECT Addr_ImportID FROM [Non-Preferred Home Addresses] WHERE CnAdrAll_1_IDKEY = " & _
                    "(SELECT Max(CnAdrAll_1_IDKEY) FROM [Non-Preferred Home Addresses] WHERE CnBio_ID = '" & rs(0) & "' AND CnAdrAll_1_DateLastChanged = #" & rs2(0) & "#);"
                    Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)
                    'MsgBox rsMaxAddressImportID(0)
                End If

            End If

        End If

        'Get Actual address line
        'DoCmd.RunSQL "SELECT * INTO " & _
        "[Tbl_Max_Non-Preferred_Address] " & _
        "FROM [Non-Preferred Home Addresses] WHERE [Non-Preferred Home Addresses].Addr_ImportID = '" & rsMaxAddressImportID(0) & "';"

        DoCmd.RunSQL "INSERT INTO [Tbl_Max_Non-Preferred_Address] SELECT * FROM [Non-Preferred Home Addresses] WHERE [Non-Preferred Home Addresses].Addr_ImportID = '" & rsMaxAddressImportID(0) & "';"

        rsMaxAddressImportID.Close
        rs.MoveNext
        i = i + 1

    Loop
Else
    MsgBox "There are no records in the recordset."
End If

rs.Close
  • " Home"地址是,有些成分没有" Home"地址(见CnBio_ID 1223144)。
  • rs是选择要循环的唯一CnBio_ID。

经过Thomas G的解决方案后更新:

总的来说,一切似乎都取得了不错的结果,但我认为按顺序排序/排序问题,我似乎无法正确测试语法。我一直收到语法错误。我不熟悉括号的访问解释等,以使其正确。我试图运行以下内容:

strSql_1 = "INSERT INTO [Table1] SELECT 1 AS priority, T1.CnBio_ID, T2.Max_Date, MAX(T1.Addr_IDKEY) AS MAX_IDKey " & _
        "FROM [TB - Non-Preferred Addresses] T1 " & _
        "INNER JOIN " & _
        "(" & _
            "SELECT CnBio_ID , MAX(CnAdrAll_1_DateLastChanged) AS Max_Date " & _
            "FROM [TB - Non-Preferred Addresses] " & _
            "WHERE (Addr_Type = 'Home' AND CnBio_ID = '0106228') " & _
            "GROUP BY CnBio_ID " & _
        ") T2 ON T1.CnBio_ID = T2.CnBio_ID AND T1.CnAdrAll_1_DateLastChanged = T2.Max_Date " & _
        "WHERE Addr_Type = 'Home' " & _
        "GROUP BY T1.CnBio_ID, T2.Max_Date "
strSql_2 = " UNION ALL SELECT 2 AS priority, T1.CnBio_ID, T2.Max_Date, MAX(T1.Addr_IDKEY) AS MAX_IDKey " & _
        "FROM [TB - Non-Preferred Addresses] T1 " & _
        "INNER JOIN " & _
        "(" & _
            "SELECT CnBio_ID , MAX(CnAdrAll_1_DateLastChanged) AS Max_Date " & _
            "FROM [TB - Non-Preferred Addresses] " & _
            "WHERE (Addr_Type IN ('Campus','Harris Home', 'NCOA2003','Other','Previous','Previous Home','Previous Student','Seasonal','Student') AND CnBio_ID = '0106228') " & _
            "GROUP BY CnBio_ID " & _
        ") T2 ON T1.CnBio_ID = T2.CnBio_ID AND T1.CnAdrAll_1_DateLastChanged = T2.Max_Date " & _
        "WHERE (Addr_Type IN ('Campus','Harris Home', 'NCOA2003','Other','Previous','Previous Home','Previous Student','Seasonal','Student') AND T1.CnBio_ID = '0106228') " & _
        "GROUP BY T1.CnBio_ID, T2.Max_Date " & _
        "ORDER BY CnBio_ID, priority"

1 个答案:

答案 0 :(得分:1)

这应该可以在纯SQL中使用

SELECT V.CnBio_ID, FIRST(T.Addr_ImportID) AS Addr_ImportID , FIRST(V.Max_Date) AS CnAdrAll_1_DateLastChanged, FIRST(V.MAX_IDKey) AS CnAdrAll_1_IDKEY 
FROM [Non-Preferred Home Addresses] T
INNER JOIN
(

    SELECT 1 AS priority,  T1.CnBio_ID, T2.Max_Date, MAX(T1.CnAdrAll_1_IDKEY) AS MAX_IDKey
    FROM [Non-Preferred Home Addresses] T1
    INNER JOIN
    (
        SELECT CnBio_ID , MAX(CnAdrAll_1_DateLastChanged) AS Max_Date
        FROM [Non-Preferred Home Addresses]
        WHERE Addr_Type = 'Home'
        GROUP BY CnBio_ID
    ) T2 ON T1.CnBio_ID=T2.CnBio_ID AND T1.CnAdrAll_1_DateLastChanged=T2.Max_Date
    WHERE Addr_Type = 'Home'
    GROUP BY T1.CnBio_ID, T2.Max_Date

    UNION ALL

    SELECT 2 AS priority, T1.CnBio_ID, T2.Max_Date, MAX(T1.CnAdrAll_1_IDKEY) AS MAX_IDKey
    FROM [Non-Preferred Home Addresses] T1
    INNER JOIN
    (
        SELECT CnBio_ID , MAX(CnAdrAll_1_DateLastChanged) AS Max_Date
        FROM [Non-Preferred Home Addresses]
        WHERE Addr_Type <> 'Home'
        GROUP BY CnBio_ID
    ) T2 ON T1.CnBio_ID=T2.CnBio_ID AND T1.CnAdrAll_1_DateLastChanged=T2.Max_Date
    WHERE Addr_Type <> 'Home'
    GROUP BY T1.CnBio_ID, T2.Max_Date

    ORDER BY CnBio_ID, priority

) V 
ON T.CnBio_ID = V.CnBio_ID AND T.CnAdrAll_1_DateLastChanged=V.Max_Date AND T.CnAdrAll_1_IDKEY = V.MAX_IDKey
GROUP BY V.CnBio_ID

对于表中的每个CnBio_ID,它将保留与日期逻辑匹配的记录。您可以从中查询(查询),然后使用CnBio_ID =任意查询它以检索任何CnBio_ID的信息

但是我不知道性能是否可以接受,主要取决于表格中的记录数量。

你应该有索引:

  • CnBio_ID
  • Addr_Type
  • CnAdrAll_1_IDKEY
  • CnAdrAll_1_DateLastChanged

如果您的类型数量有限,不是“主页”,那么从一个观点来看,明确地将它们全部命名而不是搜索“<> 'HOME'”要好得多,所以你必须改变这个:

WHERE Addr_Type <> 'Home'

到这个

WHERE Addr_Type IN ('Previous','Other')

(2次)

如果你真的不明白会发生什么,如果你想要一些解释,请告诉我。 我不想无所事事地进行长篇解释。 首先分解并运行所有子查询,你应该更好地理解它。

更新

要在代码中使用我的查询,请不要在VBA中使用我的查询!使用查询设计器从中创建一个Query(在标准SQL中,这称为VIEW)。 Query Designer Example in MS Access 2016

当您在设计器中时,请切换到SQL View。 COpy /粘贴我的查询并保存。将其命名为Instance QRY_foo

现在它将作为一个表格

然后在您的VBA中,执行此操作:

strSql2 = "SELECT * FROM QRY_Foo"
Set rsMaxAddressImportID = DBEngine(0)(0).OpenRecordset(strSql2, dbOpenDynaset)

然后,您可以更轻松地调整查询,只需从查询设计器中进行调整即可。