?
Solved

Need Help with SELECT sub query...

Posted on 2008-06-25
4
Medium Priority
?
216 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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