Solved

Need Help with SELECT sub query...

Posted on 2008-06-25
4
209 Views
Last Modified: 2010-04-21
I have a select query:
"Select  company, productid, price  from sales where productid IN (select productid from sales group by productid)  order by productid, price".
However I want retrieve only the top 3 companies per productid with the highest spend (price).

I am looking to achieve this with one query if possible?
0
Comment
Question by:kirkheaton25
  • 2
4 Comments
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 21867703
Hi kirkheaton25,

Try:

Select  company, productid, price  from sales where productid IN (select TOP 3 productid from sales group by productid ORDER BY SUM(Price) DESC)  order by productid, price

Hope this helps,

Mark.
0
 
LVL 1

Author Comment

by:kirkheaton25
ID: 21868176
Thanks, however this this seems to be returning only the top 3 products). I want the top 3 companies for each productid.

Here's a simple example of the results I'm trying to get. (I hope this makes it clearer.)
Table Sales:
company |  productid  | price
-----------------------------
company1 shampoo 350
company2 shampoo 230
company3 shampoo 120
comany2 biscuits 450
company5 biscuits 230
company1 biscuits 120
company12 cheese 350
company24 cheese 230
company3 cheese 120

(This is for each individual product id is the table sales)
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21868190
try this (will only work on sql 2005..which you posted)


select * from
(
Select  company, productid, price, ranking = dense_rank() over(partition by productid order by price desc)
from sales
) a
where ranking <= 3
0
 
LVL 1

Author Closing Comment

by:kirkheaton25
ID: 31470650
Brilliant, exactly what I wanted!
(I might even understand what's going on one day!)
Cheers.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now