Solved

Order and Group the same query

Posted on 2009-06-30
7
1,105 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 52
compare date to getdate() 8 16
using a SELECT statement in a  THEN  of a CASE statement. 5 14
SQL query 7 14
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

756 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