Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

can we use ORDER BY in a Sub-query ?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: can we use ORDER BY in a Sub-query ?
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    Post subject:
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

Active Member


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

PostPosted: Thu Mar 13, 2008 10:46 pm    Post subject:
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    Post subject: Reply to: can we use ORDER BY in a Sub-query ?
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    Post subject: Reply to: can we use ORDER BY in a Sub-query ?
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

Site Director


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

PostPosted: Fri Mar 14, 2008 9:47 pm    Post subject:
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    Post subject: Reply to: can we use ORDER BY in a Sub-query ?
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    Post subject:
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: 6968
Location: porcelain throne

PostPosted: Tue Mar 18, 2008 12:39 pm    Post subject:
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    Post subject: Reply to: can we use ORDER BY in a Sub-query ?
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    Post subject:
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    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Omegamon for DB2 query ashek15 DB2 0 Wed Feb 15, 2017 11:25 am
No new posts Query on DB2 WLM stored procedures ashek15 DB2 8 Sun Feb 12, 2017 11:13 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us