Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / November 2005

Tip: Looking for answers? Try searching our database.

TOP 20 PER Marketplace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GEORGIA - 16 Nov 2005 19:15 GMT
Hello I have following SQL:
SELECT qryTempeAccounts.*
FROM qryTempeAccounts
WHERE qryTempeAccounts.[Total Over 60] In   (   SELECT TOP 20 T.[Total Over
60] from qryTempeAccounts As T    WHERE T.marketplace =
qryTempeAccounts.marketplace   ORDER BY [Total Over 60] DESC )
ORDER BY [qryTempeAccounts].[marketplace];

What I am trying to do is pull top 20 per marketplace based on the highest
over 60 days amount.  (top 20 highest amount for each marketplace)
Table itself only have 73,000 records ( I don't considered this alot for
Access). I tried running it and it's been 30 minutes and still no result!  
Anyone has better idea how I can get this query to run??

thank you!
John Spencer - 16 Nov 2005 19:52 GMT
Make sure you have indexes on proper fields.  But I suspect that the problem
has to do with qryTempeAccounts having to run multiple times.    Perhaps
that can be optimized in the subquery.  Also, if qryTempeAccounts has an
ORDER BY clause drop it.

Suggest you post the code for qryTempeAccounts in addition to what you
already have posted.  Wild guess that the subquery might end up looking
something LIKE the following

In (SELECT TOP 20 AccountID
    FROM Sometable as T
    WHERE T.Marketplace = qryTmpeAccounts.Marketplace
    GROUP BY AccountID
    ORDER BY Sum(Amount) DESC)

SUM(Amount)
> Hello I have following SQL:
> SELECT qryTempeAccounts.*
[quoted text clipped - 12 lines]
>
> thank you!
Michel Walsh - 16 Nov 2005 19:54 GMT
Hi,

You have an index on marketPlace and on [Total Over 60] ?   Probably not,
since you call the table  "qryTempeAccount", which is, I suppose, a query.
Make a temporary table out of the query, and index the mentioned fields.

If this it still too slow, try to join (to compute a rank) rather than to
"sub-query":

SELECT a.marketPlace, a.totalOver60
FROM myTable As a LEFT JOIN myTable as b
   ON a.marketPlace = b.marketPlace AND a.totalOver60 < b.totalOver60
GROUP BY a.marketPlace, a.totalOver60
HAVING COUNT(b.totalOver60)  < 10

In that query, COUNT(b.totalOver60) supplies  the rank, within the same
marketPlace, occupied by the value a.totalOver60.  I use a LEFT join and
strict <  comparisons to include all the ex-equo that could eventually fill
position 10.

Hoping it may help,
Vanderghast, Access MVP

> Hello I have following SQL:
> SELECT qryTempeAccounts.*
[quoted text clipped - 12 lines]
>
> thank you!
GEORGIA - 17 Nov 2005 16:00 GMT
Thank you both!!
It runs so much quicker! !

> Hi,
>
[quoted text clipped - 35 lines]
> >
> > thank you!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.