Solved

use inner query result in main query order by

Posted on 2011-03-08
6
300 Views
Last Modified: 2012-05-11
Hello experts,

i have inner queries results in main query and i want to sort that inner query result using order by tag in main query.

say for example:

Tablename:  Product

Tablename: Product_review

Relation: both tables connected using Property table id and Product_review table productid

so it will come like

Select p.*,(select Count(*) from Product_review pr where pr.productid=p.id) as pro_review from Product p

Now what i want is something like :

Select p.*,(select Count(*) from Product_review pr where pr.productid=p.id) as pro_review from Product p order by pro_review desc

but its not working when i run its asking me pro_review value as its not part of main tables but its a part of query result.

How do we achieve it using single sql query ?

Thanks
Kinjal
0
Comment
Question by:dotsandcoms
  • 4
6 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 35068833
select * from (Select p.*,(select Count(*) from Product_review pr where pr.productid=p.id) as pro_review from Product p) a order by pro_review desc
0
 
LVL 35

Expert Comment

by:YZlat
ID: 35069008
try something like this

SELECT p.*, COUNT(pr.productid) As pro_review
FROM Product as p
INNER JOIN Product_review pr ON p.id=pr.productid
ORDER BY COUNT(pr.productid) DESC
0
 
LVL 35

Expert Comment

by:YZlat
ID: 35069017
SELECT p.*, COUNT(pr.productid) As pro_review
FROM Product as p
INNER JOIN Product_review as pr ON p.id=pr.productid
ORDER BY COUNT(pr.productid) DESC
0
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.

 
LVL 35

Expert Comment

by:YZlat
ID: 35069190
actually you cna't do p.*, you will have to list all the columns and then add a GROUP BY clause
0
 
LVL 35

Expert Comment

by:YZlat
ID: 35069198

SELECT p.id, p.ProductTitle, p.AnotherField, COUNT(pr.productid) As pro_review
FROM Product as p
INNER JOIN Product_review as pr ON p.id=pr.productid
GROUP BY p.id, p.ProductTitle, p.AnotherField
ORDER BY COUNT(pr.productid) DESC
0
 

Author Closing Comment

by:dotsandcoms
ID: 35078951
simply best
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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

776 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