Solved

order by count

Posted on 2011-02-15
8
389 Views
Last Modified: 2012-06-27
productid is int
I want to order by count of productid


select * from orderitems i
left join products p on p.productid=i.productid

0
Comment
Question by:rgb192
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 34900664
Are you looking for something like this?
select i.productid,COUNT(*) cnt
  from orderitems i
  left join products p on p.productid=i.productid
 order by cnt
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34900714
select *  
from orderitems i
left join (select productid, COUNT(1) as ProductCount
           from products
           group by productid) p on p.productid = i.productid
order by p.ProductCount
0
 

Author Comment

by:rgb192
ID: 34900721
Msg 8120, Level 16, State 1, Line 1
Column 'orderitems.productid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 34900737
I missed group by.

select i.productid,COUNT(*) cnt
  from orderitems i
  left join products p on p.productid=i.productid
 group by i.productid
 order by cnt
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34900762
Did you look at my solution #34900714
0
 

Author Closing Comment

by:rgb192
ID: 34900800
>>Did you look at my solution #34900714  
returned only null and 1 for productcount
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34900805
select *
 ,count(i.productid) over (order by productid) as ct
from orderitems i
left join products p on p.productid=i.productid
order by ct desc
0
 

Author Comment

by:rgb192
ID: 34900951
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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