Solved

Order and Group the same query

Posted on 2009-06-30
7
1,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

734 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