View previous topic :: View next topic |
Author |
Message |
pzmohanty Beginner
Joined: 20 May 2004 Posts: 97 Topics: 43 Location: hyderabad, India
|
Posted: Sun Sep 05, 2004 8:22 am Post subject: problem with UNION |
|
|
hi all,
I am facing a problem with UNION clause :
i have a following query----
SELECT COMMISSION,EMP_ID FROM EMPLOYEE
WHERE COMMISSION = 0 FETCH FIRST ROW ONLY
UNION
SELECT COMMISSION,EMP_ID FROM EMPLOYEE
WHERE COMMISSION = 100 FETCH FIRST ROW ONLY ;
The error message i got is following:
DSNT408I SQLCODE = -199, ERROR: ILLEGAL USE OF KEYWORD UNION, TOKEN
<END-OF-STATEMENT> WAS EXPECTED
DSNT418I SQLSTATE = 42601 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNHPARS SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 0 0 0 -1 147 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'00000000' X'00000000' X'00000000' X'FFFFFFFF'
X'00000093' X'00000000' SQL DIAGNOSTIC INFORMATION
How else can i get the desired result??
Following is the requirement :
The EMPLOYEE table has various columns , 2 of them are EMP_ID & COMMISSION.
EMP_ID is primary key.
in the table there are many employees have commission of 0 & 100.
there are other employees too had commissions other than 0 & 100.
I have to query EMP_ID & COMMISSION of one employee each for employee getting commission 0 & employee getting commission 100.
Thanx in advnc
P.R.Mohanty |
|
Back to top |
|
|
pzmohanty Beginner
Joined: 20 May 2004 Posts: 97 Topics: 43 Location: hyderabad, India
|
Posted: Mon Sep 06, 2004 6:14 am Post subject: |
|
|
hi all,
one way i found to get around the problem is to use following query :
SELECT EMP_ID , COMMISSION FROM EMPLOYEE
WHERE EMP_ID = ( SELECT MAX(EMP_ID) FROM EMPLOYEE WHERE COMMISSION = 0 )
UNION
SELECT EMP_ID , COMMISSION FROM EMPLOYEE
WHERE EMP_ID = ( SELECT MAX(EMP_ID) FROM EMPLOYEE WHERE COMMISSION = 100 )
but I believe this is CPU hungry quey;
Is there any better query ???
Thanx ,
P.R.Mohanty |
|
Back to top |
|
|
a_seshu Beginner
Joined: 30 Sep 2004 Posts: 16 Topics: 4 Location: Chennai
|
Posted: Fri Oct 01, 2004 11:39 pm Post subject: A better way of coding the SQL for your requirement. |
|
|
This would be a CPU friendly SQL
Code: | SELECT EMP_ID , COMMISSION FROM EMPLOYEE
WHERE EMP_ID IN ( SELECT MAX(EMP_ID) FROM EMPLOYEE GROUP BY COMMISSION HAVING COMMISSION IN (0,100)) |
Worked fine when i did a quick run in Oracle. I am sure it would fare well in DB2 too. _________________ I dont think I would ever stop learning. - Seshu. |
|
Back to top |
|
|
|
|