Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why am I getting additional rows?? 2

Status
Not open for further replies.

Goalie3533

Programmer
Apr 14, 2004
53
US
I have a query that should diplay the 3 top selling items from a particular product category(i.e: Clothes, Hats, Shoes, etc). I'm joining 2 tables to gather all of the necessary information however more than 3 rows are being returned for some strange reason. I'm selecting the product ID #(catalogid), total quantity of the product sold(sum(oitems.numitems)) and joining those values with my Product Categories table(prodcategories) so I can match it up with one particular category(347). Any idea what's wrong with my query that would enable it to return more than 3 rows?
----------------------------------------------
select TOP 3 oitems.catalogid, sum(oitems.numitems) as sumofnumitems from oitems inner join prodcategories on oitems.catalogid=prodcategories.intcatalogid where prodcategories.intcategoryid=347 GROUP BY oitems.catalogid ORDER BY sum(oitems.numitems) DESC
------------------------------------------------

Thanks in advance.

-Goalie35
 

Nothing wrong with the query, possibly you have equal values.

The TOP predicate does not choose between equal values, all rows with the TOP 3 values will be returned.
 
To be technically correct - The TOP predicate does not choose between equal values [blue]in the ORDER BY clause[/blue].

So as an afterthought because the catlogid is unique you should be able to correct the problem by adding the catalogid field to the ORDER BY.

I think...


 
I had a similar thing with similar code.

It doesnt seem to like a top with a group by. Do a second query to query this one, then put the top in that.

----------------------------------------
There are 2 types of computer, the prototype and the obsolete!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top