根据某些条件在单个查询中加入表A或表B.

时间:2015-03-26 10:50:42

标签: sql oracle11g

表格有交易信息。

enter image description here

grd_market_exchange_cd的值映射到不同的time_zone_name,可通过以下方式获取:

WITH time_zone AS (
    SELECT DISTINCT
            grd.grd_market_exchange_cd,
            ex.time_zone_name
        FROM mdm_code.exchange_grd_exchange_map mp
        JOIN mdm_code.grd_market_exchange grd ON grd.grd_market_exchange_cd = mp.grd_market_exchange_cd
        JOIN mdm.market_exchange ex ON ex.market_exchange_cd = mp.market_exchange_cd
        WHERE ex.deactivation_dt IS NULL
        AND EXISTS (
            SELECT 'X'
            FROM ae_os_market_exchange e
            WHERE e.market_exchange_cd = ex.market_exchange_cd
            AND e.jobs_load_id = ( SELECT MAX(jobs_load_id) FROM ae_os_market_exchange )
        )
        AND ex.time_zone_name IS NOT NULL
)
SELECT
    at.trade_id,
    at.trade_execution_tmstmp,
    at.grd_market_exchange_cd,
    tz.time_zone_name
FROM cd_data.allocated_trade at
JOIN time_zone tz ON tz.grd_market_exchange_cd = at.grd_market_exchange_cd
WHERE TRUNC(TRADE_EXECUTION_TMSTMP) = TRUNC(SYSDATE)
AND TRADE_STATUS_CD IN ('EX','TR')
AND trade_id IN (
308983463,
308983465,
308983929,
308983950,
308979467
);

enter image description here

选项1: 这是第一种方式。但以这种方式获取time_zone_name的问题是:

  • grd_market_exchange_cd在某些情况下为NULL。
  • 1 grd_market_exchange_cd在某些情况下映射到2个或更多时区。
  • grd_market_exchange_cd到time_zone_name映射不适用于某些代码。

我们每次都通过这种方式选择time_zone_name,除了上面提到的这三种情况。如上所示,如果是grd_market_exchange_cd,我会为同一笔交易获得两行 如果它是NULL或未映射,则映射到2个时区或没有行。

选项2: 如果以某种方式我没有使用grd_market_exchange_cd得到time_zone_name,那么我将不得不使用不同的表来获取值,如下所示:

SELECT
    at.trade_id,
    at.trade_execution_tmstmp,
    at.grd_market_exchange_cd,
    mme.time_zone_name
FROM cd_data.allocated_trade at
JOIN ae_os_listing_v l ON at.tradable_ent_id = l.cde_tradable_entity_id
JOIN ae_os_market_exchange me ON me.listing_key_id = l.listing_key_id
JOIN mdm.market_exchange mme ON me.market_exchange_cd = mme.market_exchange_cd
WHERE TRUNC(trade_execution_tmstmp) = TRUNC(SYSDATE)
AND at.trade_status_cd IN ('EX','TR')
AND me.jobs_load_id = (SELECT MAX(jobs_load_id) FROM ae_os_market_exchange)
AND me.global_exchange_designation_cd = 'P'
AND trade_id IN (
308983463,
308983465,
308983929,
308983950,
308979467
);

enter image description here

我必须开发合并上述两个查询的单个查询,以便我通过选项1选择time_zone_name,如果grd_market_exchange_cd被精确地映射到1个time_zone_name或者通过选项2,如果我没有通过选项1获得time_zone_name值。

0 个答案:

没有答案