Results 1 to 5 of 5
  1. #1
    therzakid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    22

    top n macro via union

    Hi All,

    I recently came across some code I thought would solve my problems. However, it doesn't. Although i get a "top 3" query for each store, it doesn't actually work. I get top 3 for the first sote, but all the others below are random. I like the code because it's dynamic, also my sales file has 100k+ records so a sub query takes a really long time.

    Here is the code I'm using:

    Code:
    Option Compare Database
    Option Explicit
    Function SelectTop3From_tbl_MoM()
        Const c_SQL As String = "SELECT TOP 3 * FROM [tbl_MoM] WHERE SiteNo = '@' and [Fiscal Year] = ""2012"" ORDER BY [Net Invoiced Revenue] DESC"
        
        Dim rst As DAO.Recordset
        Dim qdf As DAO.QueryDef
        Dim strSQL As String
        
        ' Gather all existing values for CompanySymbol.
        '
        strSQL = "SELECT DISTINCT SiteNo FROM [tbl_MoM];"
        Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
        
        ' Assemble the query.
        '
        strSQL = ""
        With rst
            Do Until .EOF
                If strSQL <> "" Then strSQL = strSQL & " UNION "
                strSQL = strSQL & Replace(c_SQL, "@", !SiteNo)
                .MoveNext
            Loop
            .Close
        End With
        Set rst = Nothing
        strSQL = "SELECT * FROM (" & strSQL & ") ORDER BY [Net Invoiced Revenue] DESC;"
        
        ' Edit the existing query object and replace its SQL property by the SQL previously assembled, then open it.
        '
        Set qdf = CurrentDb.QueryDefs("top_3_items_from_MoM")    ' There must be a query named "Qry_Top_3_Sales" in the database
                                                            ' (can be: "SELECT * FROM Tbl_Sales", only the name matters).
        qdf.SQL = strSQL
        DoCmd.OpenQuery "top_3_items_from_MoM"
        Set qdf = Nothing
        
    End Function

    And, here is a sample of the output SQL:

    SELECT *
    FROM (SELECT TOP 3 * FROM [tbl_MoM] WHERE SiteNo = '03' and [Fiscal Year] = "2012" ORDER BY [Net Invoiced Revenue] DESC UNION SELECT TOP 3 * FROM [tbl_MoM] WHERE SiteNo = '10' and [Fiscal Year] = "2012" ORDER BY [Net Invoiced Revenue] DESC UNION SELECT TOP 3 * FROM [tbl_MoM] WHERE SiteNo = '12' and [Fiscal Year] = "2012" ORDER BY [Net Invoiced Revenue] DESC UNION SELECT TOP 3 * FROM [tbl_MoM] WHERE SiteNo = '17' and [Fiscal Year] = "2012" ORDER BY [Net Invoiced Revenue] DESC) AS [%$##@_Alias]
    ORDER BY [Net Invoiced Revenue] DESC;





    Thanks for any guidance and help you might provide - much appreciated.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Could you provide some sample data so can test with? An Excel spreadsheet of about 1000 of the tbl_MoM records would be enough. Make sure there is an assortment of SiteNo and Year values.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    therzakid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    22
    Quote Originally Posted by June7 View Post
    Could you provide some sample data so can test with? An Excel spreadsheet of about 1000 of the tbl_MoM records would be enough. Make sure there is an assortment of SiteNo and Year values.
    Thanks for your reply, June. Below is the link:

    http://www.4shared.com/folder/8AUNxZQY/ pw= 5678

    I look forward to hearing any suggestions and moreover a solution! Thank you for your time.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    My experimentation shows that the ORDER BY clause is ignored after the first SELECT line of the UNION query and the sort order is applied to the entire UNION dataset. I am thinking this is a bug in the Access SQL engine. Never thought much about it but have seen this behavior before.

    I suggest an alternative is to write the top 3 records to a temp table. Purge the table each time the procedure is run.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    therzakid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    22
    I took your advice and made some code to simply delete records from the top 10 table and fill them in by running an appened query N amount of times. Thanks for the direction June7, you're great.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Union Crosstab
    By dssrun in forum Queries
    Replies: 2
    Last Post: 11-14-2011, 10:28 AM
  2. Union & union all
    By jasonbarnes in forum Queries
    Replies: 4
    Last Post: 10-27-2011, 12:30 PM
  3. Union or better way.
    By kevin28 in forum Access
    Replies: 2
    Last Post: 09-06-2011, 02:42 PM
  4. Replies: 0
    Last Post: 01-12-2011, 12:43 PM
  5. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums