Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need Help with SELECT sub query...

Posted on 2008-06-25
4
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this article, we’ll look at how to deploy ProxySQL.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

609 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