Solved

# Top 10 Results Giving Me 12 - Not 10

Posted on 2012-09-21
474 Views
I have the following code where I am trying to pull the Top 10, but it gives me 12 (instead of 10).

If I change the code to Top 9 or Top 5, that works, but the minute I change it to Top 10, it doesn't give me 10.

Here is the code:

``````SELECT DISTINCT TOP 10 [tblErrorDetails (for Report)].SectionDesc, Sum(IIf([Assoc]="Y","1","0")) AS Associate, Sum(IIf([Opers]="Y","1","0")) AS Department_, Sum(IIf([Impact]="Y","1","0")) AS Impact_
FROM [tblErrorDetails (for Report)]
GROUP BY [tblErrorDetails (for Report)].SectionDesc
ORDER BY Sum(IIf([Assoc]="Y","1","0")) DESC , Sum(IIf([Opers]="Y","1","0")) DESC , Sum(IIf([Impact]="Y","1","0")) DESC;
``````

Why does it work with 5 or 9, but not 10?

Thanks,
gdunn59
0
Question by:gdunn59

LVL 75

Expert Comment

This is likely because you have exact duplicates.

Also, why are you using both DISTINCT and GROUP BY ?

mx
0

LVL 47

Expert Comment

That is because the Order By clause has more than 10 items with the same value as the 10th item in the list.  If your tenth item had 20 records with values of 0, 1, 1 for the three indicators in the Order By clause, and 9 values prior to that in the ordered list, then the results would contain 29 records.

The only way to ensure you get only 10 would be to add more fields to the OrderBy clause
0

Author Comment

I removed the Distinct, but still having the issue.
0

LVL 75

Expert Comment

Well, yes ... because the real issue is what fyed and myself described ...

Look at your results - and you s/b able to see this ...

This is why the TOP function can be misleading ...

mx
0

Author Comment

So there is basically no way around this, correct?
0

LVL 48

Accepted Solution

There is. Run your Query using distinct or Group by - then use this Query as the source in another Query (straight select) where you apply Top 10.

/gustav
0

Author Closing Comment

This worked perfect.

Thanks,

gdunn59
0

LVL 48

Expert Comment

You are welcome!

/gustav
0

## Featured Post

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…