Solved

order by count

Posted on 2011-02-15
8
381 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

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