Solved

Need Help with SELECT sub query...

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax: How to force case sensitive query? 2 44
Merging spreadsheets 8 60
Help With Simple Database Design 7 50
table joins in qry 17 63
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

831 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