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

x
?
Solved

use inner query result in main query order by

Posted on 2011-03-08
6
Medium Priority
?
335 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
[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
  • 4
6 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

598 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