IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

can we use ORDER BY in a Sub-query ?


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
zero

New User


Joined: 01 Dec 2007
Posts: 21
Location: Hyderabad

PostPosted: Thu Mar 13, 2008 9:29 pm
Reply with quote

Hi,
my table has data like this :

DEPT_NO emp_no
---------- ----------
D01 11111
D01 11111
D02 22222
D02 22222
D03 33333

My requirement is to get all the Departments with Maximum number of Employees.

for the above example, the output should be like this :

D01 2
D02 2.

I have written a query to achieve the same. It is working in DB2 for Windows. but, it is not working in SPUFI.

Code:
SELECT dept_no, COUNT(*)
FROM   dept
GROUP  BY dept_no
HAVING COUNT(*) = (SELECT COUNT(*) AS “BB”
                          FROM   dept
                          GROUP  BY dept_no
                          ORDER  BY BB DESC
                          FETCH FIRST 1 ROW ONLY);


i am getting : SQLCODE = -199. ERROR: ILLEGAL USE OF KEYWORD ORDER.

my doubt is can't we use ORDER BY in a subquery ?
what's wrong with the above query ?

Can we make to give the result ?
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Thu Mar 13, 2008 10:30 pm
Reply with quote

What are the results when you do it in SPUFI?
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Thu Mar 13, 2008 10:46 pm
Reply with quote

Your sub-query returns a count(*), so what exactly are you ordering?

Under what circumstances does it make sense to order any sub-query?
Back to top
View user's profile Send private message
zero

New User


Joined: 01 Dec 2007
Posts: 21
Location: Hyderabad

PostPosted: Fri Mar 14, 2008 9:00 am
Reply with quote

I am getting this error :

Code:
DSNT408I SQLCODE = -199, ERROR:  ILLEGAL USE OF KEYWORD ORDER.  TOKEN ) UNION EXCEPT WAS EXPECTED                                                 
DSNT418I SQLSTATE   = 42601 SQLSTATE RETURN CODE                             
Back to top
View user's profile Send private message
zero

New User


Joined: 01 Dec 2007
Posts: 21
Location: Hyderabad

PostPosted: Fri Mar 14, 2008 9:07 am
Reply with quote

Here, My requirement is to get all the Departments with max. number of Employees. But, two depts. can have same maximum number of employees.

The work of the sub-query in this case is to find the Maximum number of Employees.

In this example, it is 2.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Mar 14, 2008 9:47 pm
Reply with quote

Hello,

I believe we understand the requirement.

I also believe that you did not understand the -199 as well as the earlier reply - as coded, the order by is invalid.

Did you run the spufi test as requested? If so, what happens?
Back to top
View user's profile Send private message
Gijz

New User


Joined: 27 Nov 2007
Posts: 9
Location: The Netherlands

PostPosted: Mon Mar 17, 2008 11:56 pm
Reply with quote

Hello zero,

You can use ORDER BY in a subquery, but not in conjunction with an aggregate function in that subselect or a group by.

Possible solution to your problem? Use a common table expression (V8 onwards):

Code:

WITH DEPARTMENT (NR_EMP) AS           
     (SELECT COUNT(*)                     
        FROM DEPT   
       GROUP BY DEPT_NO                   
     )                                   
SELECT DEPT_NO                           
      ,COUNT(*)                           
  FROM DEPT         
 GROUP BY DEPT_NO                         
HAVING COUNT(*) = (SELECT MAX(NR_EMP)
                     FROM DEPARTMENT)
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Tue Mar 18, 2008 10:46 am
Reply with quote

I suppose ORDER BY and FETCH FIRST clause can't be used together.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Mar 18, 2008 12:39 pm
Reply with quote

in vs8 yes
Back to top
View user's profile Send private message
zero

New User


Joined: 01 Dec 2007
Posts: 21
Location: Hyderabad

PostPosted: Tue Mar 25, 2008 12:16 pm
Reply with quote

Thank you for your help...
Back to top
View user's profile Send private message
sainathvinod

New User


Joined: 01 Apr 2008
Posts: 11
Location: Chennai

PostPosted: Tue Apr 01, 2008 9:01 pm
Reply with quote

Hey, you are getting the error -199 because you are not allowed to use the FETCH FIRST clause in a subquery. It can be used only in the main query as a last statement. There is nothing wrong in using the order by clause in the subquery. You can also use the combination of Order by and fetch first in the main query.
Back to top
View user's profile Send private message
sainathvinod

New User


Joined: 01 Apr 2008
Posts: 11
Location: Chennai

PostPosted: Tue Apr 01, 2008 9:49 pm
Reply with quote

Please try the below query. I hope that this solves your problem :-

SELECT DEPT_NO
,COUNT(*)
FROM DEPT
GROUP BY DEPT_NO
HAVING COUNT(*) =
(SELECT MAX(TEMP.A) FROM
(SELECT DEPT_NO
,COUNT(*) A
FROM DEPT
GROUP BY DEPT_NO) TEMP)
WITH UR;
Back to top
View user's profile Send private message
Gijz

New User


Joined: 27 Nov 2007
Posts: 9
Location: The Netherlands

PostPosted: Wed Apr 02, 2008 4:15 pm
Reply with quote

sainathvinod wrote:
Please try the below query. I hope that this solves your problem :-

SELECT DEPT_NO
,COUNT(*)
FROM DEPT
GROUP BY DEPT_NO
HAVING COUNT(*) =
(SELECT MAX(TEMP.A) FROM
(SELECT DEPT_NO
,COUNT(*) A
FROM DEPT
GROUP BY DEPT_NO) TEMP)
WITH UR;


That's basically the same query as I suggested icon_smile.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Rotate partition-logical & physic... DB2 0
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top