tags:

views:

359

answers:

3

I am trying to get the top 2 results per day, where the "top" results are the rows with the highest "MPR". My table looks like this:

date      symbol   MPR
8/7/2008  AA       0.98
8/7/2008  AB       0.97
8/7/2008  AC       0.96
...
8/7/2008  AZ       0.50
8/8/2008  AA       0.88
8/8/2008  AB       0.87
8/8/2008  AC       0.86
...
8/8/2008  AZ       0.40
...
many other days

I would like the result set to be:

date      symbol   MPR
8/7/2008  AA       0.98
8/7/2008  AB       0.97
8/8/2008  AA       0.88
8/8/2008  AB       0.87

I have tried using the TOP keyword, but this only gives me the top 2 rows, and I'm trying to do the top 2 rows per date.

I am using Microsoft Access. I'd appreciate any help! :-)

+1  A: 

http://www.rogersaccesslibrary.com/forum/forum%5Fposts.asp?TID=233

Check page 5

Here is the solution:

SELECT t1.date, t1.symbol, t1.MPR
FROM table1 t1
WHERE t1.MPR IN
(
  SELECT TOP 2 t2.MPR FROM table1 t2
  WHERE
  t2.date = t1.date
  ORDER BY t2.MPR DESC
)
DmitryK
Also check this article - quite self-explanatory (although the syntax used is not MS Access but gives you a clear idea of what to do)http://www.bennadel.com/blog/1114-Selecting-Top-X-From-Each-Group.htm
DmitryK
Thanks, DmitryK. I haven't been able to get that to work yet, but I'll keep on trying...
bobbyh
No worries. If you want - post what you've got so far and we might be able to help...
DmitryK
Thanks, DmitryK, this is what I ended up with after reading that word doc on page 5! :-)
bobbyh
Note: this query was incredibly slow to run.
bobbyh
Do you have indices (especially the one on the MPR field)?Also check other solutions from that doco - the ones that use temp tables. They are supposed to run faster. But I'd start with checking correct indices.
DmitryK
A: 

Something like..

     SELECT DISTINCT(symbol), date, MAX(MPR) 
     FROM [TABLE]
     GROUP BY date,symbol

should do it... or symbol,date switched in the GROUP BY.

SirStan
no, it will return only 1 (MAX) value of MPR. Bobbyh needs 2 values returned.
DmitryK
Right -- details details -- I deleted my answer after I re-read his question :).. but you commented first so I undeleted.
SirStan
+1  A: 

Here you go:

    SELECT YourTable.DateStamp, YourTable.Symbol, Max(YourTable.MPR) AS MaxOfMBR
    FROM YourTable
    GROUP BY YourTable.DateStamp, YourTable.Symbol, YourTable.MPR
    HAVING YourTable.MPR In (SELECT TOP 2 MPR FROM YourTable T2 WHERE 
    YourTable.DateStamp = T2.DateStamp ORDER BY MPR DESC)
    ORDER BY YourTable.DateStamp, YourTable.MPR DESC;

The trick here is to use an aggregate query, and then in the HAVING statement (basically the "WHERE" of an aggregate query), you use a subquery to pull the top 2 values from the MPR column.

I made a quick DB in Access 2007 and this worked fine.

EDIT: Clarification on the usage of MAX here. You're returning the MAX of a group, not the MAX value of the column for a particular day. Therefore, you get the correct number of records (2) per date.

Sarkazein