Hi,
I'm trying to determine the best way in which to replace an old query. The tables and fields that were used in the old query no longer exist due to database re-design. It has been suggested to me by a more senior developer, that I use DECODE or CASE. The old query is as follows:
SELECT targ_id, sel_id, sel_name, 'EML' AS "SELECTOR_TYPE", date_added, date_modified, date_deleted
FROM tbl_EMAIL
UNION
SELECT sel_id, sel_name, 'NET' AS "SELECTOR_TYPE",
date_added, date_modified, date_deleted
FROM tbl_NETWK
UNION
SELECT sel_id, sel_name, 'NUM' AS "SELECTOR_TYPE",
date_added, date_modified, date_deleted
FROM tbl_NUMBER
As I mentioned, the database has been re-designed making several of the tables obselete. As well, the date_added, date_deleted and date_modified fields have been replaced with a LOG table which records the action and the date of the associated action. The SELECTOR_TYPE has been replaced with a GENERAL_TYPES table that stores an GENERAL_TYPE_ID and GENERAL_TYPE (description) of the ID. My attempt at re-writing the query is as follows:
SELECT SEL.SEL_ID, SEL.SEL_NAME,SEL.GENERAL_TYPE_ID,
GENERAL_TYPES.GENERAL_TYPE,SEL_LOG.SEL_ID, SEL_LOG.DATES,
SEL_LOG.ACTION
FROM SEL, GENERAL_TYPES, SEL_LOG
WHERE SEL.GENERAL_TYPE_ID = GENERAL_TYPES.GENERAL_TYPE_ID
AND SEL.SEL_ID = SEL_LOG.SEL_ID
AND SEL.GENERAL_TYPE_ID IN (1,2,3)
I used the IN function with the numbers 1 thru 3 because they correspond with the values EMAIL, NETWORK and NUMBER in the GENERAL_TYPES table. My query does seem to work fine, in the sense that it brings back the expected data. The senior developer suggested to me that the IN function is not a good idea as the values related to the numbers I have used may change. This is where the subject of using DECODE or CASE came up. I'm kind of familiar with how to do use DECODE or CASE, I'm just not sure how to apply it to my situation. The values in the GENERAL_TYPES table is as follows:
GENERAL_TYPE_ID GENERAL_TYPE
1 EMAIL
2 NETWORK
3 NUMBER
Any suggestions about how to proceed would be appreciated.
Sorry for the long post!
Scott.
I'm trying to determine the best way in which to replace an old query. The tables and fields that were used in the old query no longer exist due to database re-design. It has been suggested to me by a more senior developer, that I use DECODE or CASE. The old query is as follows:
SELECT targ_id, sel_id, sel_name, 'EML' AS "SELECTOR_TYPE", date_added, date_modified, date_deleted
FROM tbl_EMAIL
UNION
SELECT sel_id, sel_name, 'NET' AS "SELECTOR_TYPE",
date_added, date_modified, date_deleted
FROM tbl_NETWK
UNION
SELECT sel_id, sel_name, 'NUM' AS "SELECTOR_TYPE",
date_added, date_modified, date_deleted
FROM tbl_NUMBER
As I mentioned, the database has been re-designed making several of the tables obselete. As well, the date_added, date_deleted and date_modified fields have been replaced with a LOG table which records the action and the date of the associated action. The SELECTOR_TYPE has been replaced with a GENERAL_TYPES table that stores an GENERAL_TYPE_ID and GENERAL_TYPE (description) of the ID. My attempt at re-writing the query is as follows:
SELECT SEL.SEL_ID, SEL.SEL_NAME,SEL.GENERAL_TYPE_ID,
GENERAL_TYPES.GENERAL_TYPE,SEL_LOG.SEL_ID, SEL_LOG.DATES,
SEL_LOG.ACTION
FROM SEL, GENERAL_TYPES, SEL_LOG
WHERE SEL.GENERAL_TYPE_ID = GENERAL_TYPES.GENERAL_TYPE_ID
AND SEL.SEL_ID = SEL_LOG.SEL_ID
AND SEL.GENERAL_TYPE_ID IN (1,2,3)
I used the IN function with the numbers 1 thru 3 because they correspond with the values EMAIL, NETWORK and NUMBER in the GENERAL_TYPES table. My query does seem to work fine, in the sense that it brings back the expected data. The senior developer suggested to me that the IN function is not a good idea as the values related to the numbers I have used may change. This is where the subject of using DECODE or CASE came up. I'm kind of familiar with how to do use DECODE or CASE, I'm just not sure how to apply it to my situation. The values in the GENERAL_TYPES table is as follows:
GENERAL_TYPE_ID GENERAL_TYPE
1 EMAIL
2 NETWORK
3 NUMBER
Any suggestions about how to proceed would be appreciated.
Sorry for the long post!
Scott.