Solved

Order and Group the same query

Posted on 2009-06-30
7
1,101 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 32

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

26 Experts available now in Live!

Get 1:1 Help Now