Solved

Order and Group the same query

Posted on 2009-06-30
7
1,099 Views
Last Modified: 2012-05-07
I'm trying to create a query which provides me with a list of the top 20 suppliers of our company.  This information is stored in an MSSQL database.

I can count the number of orders against each supplier and list them by using group.  I would then like to order this descending, so I see the top 20 suppliers and limit it so the others are disguarded.

As soon as I add the order function I get an error.

Can anyone tell me where I'm going wrong?

Thanks
SELECT     CardCode, COUNT(*) AS POs

FROM         OPOR

WHERE     (DocDate >= '06/01/2009') AND (DocDate <= '06/30/2009')

ORDER BY POs DESC

GROUP BY CardCode

Open in new window

0
Comment
Question by:Richie4236
7 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 125 total points
ID: 24746629
SELECT     CardCode, COUNT(*) AS POs
FROM         OPOR
WHERE     (DocDate >= '06/01/2009') AND (DocDate <= '06/30/2009')
GROUP BY CardCode
ORDER BY POs DESC
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24746632
SELECT CardCode, COUNT(*) AS POs
FROM OPOR
WHERE (DocDate >= '06/01/2009') AND (DocDate <= '06/30/2009')
GROUP BY CardCode
ORDER BY COUNT(*) DESC
0
 
LVL 4

Expert Comment

by:j_s_kelley
ID: 24746661
Orber by goes after the Group By.  In fact, Order By really should be the last thing in a query.  It can't exist in a subquery or a group by operation.  The only way you can use order by in a sub query is if you are using the TOP modifier.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 31

Expert Comment

by:awking00
ID: 24748022
See attached.
query.txt
0
 
LVL 3

Expert Comment

by:BrightApollo
ID: 24751127
Here's a portable ANSI solution.

SELECT o1.CardCode, COUNT(o1.*) AS POs
FROM OPOR o1
WHERE (SELECT COUNT(o2.*) as POs FROM OPOR o2
              WHERE o1.POs > o2.POs
               AND o2.DocDate >= '06/01/2009' AND o2.DocDate <= '06/30/2009') < 21
WHERE o1.DocDate >= '06/01/2009' AND o1.DocDate <= '06/30/2009'
ORDER BY POs DESC

At this point if you wanted to group the results you can use the above as a table query.

--#
0
 
LVL 3

Expert Comment

by:BrightApollo
ID: 24756469
This is weird, the accepted answer doesn't limit to 20 rows, it just rewrites the SQL syntax to execute.
0
 

Author Comment

by:Richie4236
ID: 24758763
The question / post was mainly about grouping and ordering, as the title suggests.  The answer I accepted fixed my problem.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 123
Full Text Search string 5 33
SQL JOIN 6 39
while loop in html mail format 5 34
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

10 Experts available now in Live!

Get 1:1 Help Now