Solved

use inner query result in main query order by

Posted on 2011-03-08
6
314 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 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

726 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