how to rewrite this query [message #417727] |
Tue, 11 August 2009 05:02 |
chaituu
Messages: 115 Registered: June 2008
|
Senior Member |
|
|
SELECT EMP.EMPNO
(SELECT EMP.AMOUNT FROM EMP_MASTER EMP1
WHERE EMP1.EMPCODE= EM.EMPCODE
and EMP1.CODE IN
(SELECT DISTINCT (CASE
WHEN (DTL.CODE = 'SBA' OR
DTL.CODE = 'BAS') THEN
'BAS'
WHEN (DTL.CODE = 'SCS' OR
DTL.CODE = 'CHA') THEN
'CHA'
WHEN (DTL.CODE = 'SOS' OR
DTL.CODE = 'CUS') THEN
'CUS'
ELSE
DTL.CODE
END)
FROM EMP_DETAILS DTL
WHERE DTL.EMPCODE = EM.EMPCODE
AND DTL.INDICATOR = 'E'
AND DTL.EMPID = EM.EMPID
)) AMOUNT
FROM EMP_MASTER EM;
In the above query below part is taking lot of time ;i created indexes on EMPCODE and EMPID but it taking lot of time.how to rewrite this DISTINCT CASE query.
(SELECT DISTINCT (CASE
WHEN (DTL.CODE = 'SBA' OR
DTL.CODE = 'BAS') THEN
'BAS'
WHEN (DTL.CODE = 'SCS' OR
DTL.CODE = 'CHA') THEN
'CHA'
WHEN (DTL.CODE = 'SOS' OR
DTL.CODE = 'CUS') THEN
'CUS'
ELSE
DTL.CODE
END)
FROM EMP_DETAILS DTL
WHERE DTL.EMPCODE = EM.EMPCODE
AND DTL.INDICATOR = 'E'
AND DTL.EMPID = EM.EMPID
)
|
|
|
|
Re: how to rewrite this query [message #417730 is a reply to message #417727] |
Tue, 11 August 2009 05:13 |
cookiemonster
Messages: 13960 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
In sub-queries never need DISTINCT - so remove it and see what happens.
And next time you post a query make sure it's syntax is correct - that one won't compile.
|
|
|
|