I need to get the largest and 2nd largest amounts in a column for each postcode.
I then want to add these two figures to a table which has other information for each postcode (e.g. counts, totals, etc).
I can get largest fine:
And update the table where the postcodes match.
And I get get the top 2 overall:
And the top 2 for a specific postcode, but its way too slow to run this code for every single postcode in Excel (I tried).
For each postcode:
I'm having no luck with google trying to get second largest by postcode. The tables are in Access, so it's Access SQL. Would you be able to tell me what my code should look like? It needs to be run regularly from an Excel VBA macro, with the results being put into an Excel sheet.
If there is a tie at 1, I'd like the same number in largest and 2nd largest.
I then want to add these two figures to a table which has other information for each postcode (e.g. counts, totals, etc).
I can get largest fine:
Code:
SELECT [4_digit_postcode], max([Total_SI]) AS [LargestRiskSI] INTO tLargestRiskPerPostcode FROM [Location file_DO] GROUP BY [4_digit_postcode];
And I get get the top 2 overall:
Code:
SELECT top 2 Total_SI FROM [Location file_DO] ORDER BY Total_SI DESC;
For each postcode:
Code:
SELECT top 2 TotalSI FROM [Location file_DO] WHERE Postcode = **variablePostcode** ORDER BY TotalSI DESC
If there is a tie at 1, I'd like the same number in largest and 2nd largest.
Comment