0

I made the following group by query :

SQL = " SELECT DISTINCTROW [A], [B], [C],[D], Sum([E]) AS [Sum E]" & _
      " FROM [SheetName$RangeAddress]" & _
      " GROUP BY [A], [B], [C],[D];"

This returns a too large amount of data where I wish to have only the TOP 5 so I tried the following:

SQL = " SELECT TOP 5 FROM (SELECT DISTINCTROW [A], [B], [C],[D], Sum([E]) AS [Sum E]" & _
      " FROM [SheetName$RangeAddress]" & _
      " GROUP BY [A], [B], [C],[D]) ORDER BY [Sum E] DESC;"

This throws the following error:

The SELCT Statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect

1 Answer 1

1

Try this:

SQL = " SELECT TOP 5 * FROM" & _
      "     (SELECT DISTINCTROW [A], [B], [C], [D], Sum([E]) AS [Sum E]" & _
      "     FROM [SheetName$RangeAddress]" & _
      "     GROUP BY [A], [B], [C], [D]) AS T"
      " ORDER BY T.[Sum E] DESC;"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.