Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's highly possible that your original query is slow because of the use of sub-SELECT queries in the WHERE clause that may be executed for each record in your <code>Sort</code> table.</p> <p>If I where you, I would use VBA to reduce each <code>SELECT TOP 1</code> to the single value they are checking, and then re-inject that into the main query.</p> <p></p> <pre><code>Public Function GetMessyQuerySQL() As String ' First, extract our where-clause data SortDate, SortShift, and ID' Dim sql As String sql = sql &amp; "SELECT TOP 1 sortdate, sortshift, id " sql = sql &amp; "FROM Sort " sql = sql &amp; "WHERE (OccurrenceID = OccurrenceID) " sql = sql &amp; " AND (Sort.repaired &lt;&gt; 0) " sql = sql &amp; " OR (Sort.scrapped&lt;&gt;0) " sql = sql &amp; "ORDER BY sortdate DESC, " sql = sql &amp; " sortshift DESC, " sql = sql &amp; " id DESC;" Dim SortDate as Variant, SortShift As Variant, ID as Variant ' We assume there is always a valid data return by that query! ' Set rs = CurrentDb().OpenRecordset(sql, dbOpenForwardOnly) ' Convert dates to litteral dates in the format #29/03/2012# ' SortDate = Format(rs(0), "\#mm\/dd\/yyyy\#") SortShift = Format(rs(1), "\#mm\/dd\/yyyy\#") SortID = rs(2) rs.Close Set rs = Nothing ' Now, modify the main query with our data from the first ' sql = vbNullString sql = sql &amp; "SELECT OccurrenceID, " sql = sql &amp; " SUM(Sorted) AS SumOfSorted " sql = sql &amp; "FROM Sort " sql = sql &amp; "WHERE (SortDate &gt; %SortDate) " sql = sql &amp; " OR ((SortDate &gt;= %SortDate) " sql = sql &amp; " AND (SortShift &gt; %SortShift)) " sql = sql &amp; " OR ((SortDate = %SortDate) " sql = sql &amp; " AND (SortShift = %SortShift) " sql = sql &amp; " AND (ID &gt; %SortID)) " sql = sql &amp; "GROUP BY OccurrenceID; " ' Replace the %xxx% by their proper values ' sql = Replace(sql, "%SortDate", SortDate) sql = Replace(sql, "%SortShift", SortShift) sql = Replace(sql, "%SortID", SortID) ' Just return the constructed SQL ' GetMessyQuerySQL = SQL End Sub </code></pre> <p>So calling this function would get you SQL code like that:</p> <pre><code>SELECT OccurrenceID, SUM(Sorted) AS SumOfSorted FROM Sort WHERE (SortDate &gt; #17/02/2012#) OR ((SortDate &gt;= #17/02/2012#) AND (SortShift &gt; #29/03/2012#)) OR ((SortDate = #17/02/2012#) AND (SortShift = #29/03/2012#) AND (ID &gt; 25)) GROUP BY OccurrenceID; </code></pre> <p>Anyway that's just the basic principle, you can take it further and if your query is bound to a datasheet form for instance, you can do something like that in the Open event of the form:</p> <pre><code>Private Sub Form_Load() Me.RecordSource = GetMessySQL() End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload