View previous topic :: View next topic
|
Author |
Message |
zero
New User
Joined: 01 Dec 2007 Posts: 21 Location: Hyderabad
|
|
|
|
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 |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
What are the results when you do it in SPUFI? |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1050 Location: Richmond, Virginia
|
|
|
|
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 |
|
|
zero
New User
Joined: 01 Dec 2007 Posts: 21 Location: Hyderabad
|
|
|
|
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 |
|
|
zero
New User
Joined: 01 Dec 2007 Posts: 21 Location: Hyderabad
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Gijz
New User
Joined: 27 Nov 2007 Posts: 9 Location: The Netherlands
|
|
|
|
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 |
|
|
Srihari Gonugunta
Active User
Joined: 14 Sep 2007 Posts: 295 Location: Singapore
|
|
|
|
I suppose ORDER BY and FETCH FIRST clause can't be used together. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
in vs8 yes |
|
Back to top |
|
|
zero
New User
Joined: 01 Dec 2007 Posts: 21 Location: Hyderabad
|
|
|
|
Thank you for your help... |
|
Back to top |
|
|
sainathvinod
New User
Joined: 01 Apr 2008 Posts: 11 Location: Chennai
|
|
|
|
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 |
|
|
sainathvinod
New User
Joined: 01 Apr 2008 Posts: 11 Location: Chennai
|
|
|
|
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 |
|
|
Gijz
New User
Joined: 27 Nov 2007 Posts: 9 Location: The Netherlands
|
|
|
|
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 |
|
Back to top |
|
|
|