Solved

order by count

Posted on 2011-02-15
8
365 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now