Solved

Need Help with SELECT sub query...

Posted on 2008-06-25
4
208 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now