Oracle - SubQuery返回多行

时间:2013-01-09 16:00:04

标签: sql database oracle

我有以下表结构:

HSM

HSM_EXC_CODE    Y   VARCHAR2(60)
HSM_INSTR_CODE  Y   VARCHAR2(60)
HSM_ISIN    Y   VARCHAR2(60)
HSM_VWD_TICKERSYMBL Y   VARCHAR2(80)

TENFORE_EXCHANGE_MAP

HS_MARKET   Y   VARCHAR2(40)
TF_EXCHANGE Y   VARCHAR2(40)

TFV

TFE_ID  Y   NUMBER(22)
TFE_VSE_CODE    Y   VARCHAR2(1000)

不同的TFE_ID可以拥有相同的TFE_VSE_CODE!我认为这是我在下面的更新查询中遗漏的内容。

VSD

VSD_ON  Y   VARCHAR2(160)
VSD_ISIN    Y   VARCHAR2(15)

表格连接如下:

TENFORE_EXCHANGE_MAP.HS_MARKET   = HSM.HSM_EXC_CODE
TENFORE_EXCHANGE_MAP.TF_EXCHANGE = TFV.TFE_ID

我正在尝试填充hsm_isin和hsm_on字段。为了达到目标,我正在尝试从hsm.hsm_exc_code生成名称。 tfv.tfe_vse_code。但我做错了,因为我从主题中得到了错误。这就是我的尝试:

 UPDATE hsm
       SET hsm_isin               =
           (SELECT distinct vsd.vsd_isin
              FROM vsd, tfv, TENFORE_EXCHANGE_MAP
             WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
                   AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
                   AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id)
          ,hsm.hsm_vwd_tickersymbl =
           (SELECT distinct vsd.vsd_on
              FROM vsd, tfv, TENFORE_EXCHANGE_MAP
             WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
                   AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
                   AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id);

1 个答案:

答案 0 :(得分:0)

第一个或第二个子查询中的键必须有多行: 尝试类似:

SELECT hsm.hsm_instr_code, 
     count( distinct( vsd.vsd_on ) ) cnt1, 
     count( distinct( vsd.vsd_isin ) ) cnt2
FROM vsd, tfv, TENFORE_EXCHANGE_MAP, hsm
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id
GROUP BY hsm.hsm_instr_code
HAVING count( distinct( vsd.vsd_on ) ) > 1 OR count( distinct( vsd.vsd_isin ) ) > 1

注意:修复多行问题后,您可以将两个子查询简化为一个,如下所示:

UPDATE hsm SET ( hsm_isin, hsm.hsm_vwd_tickersymbl ) =
           (SELECT distinct vsd.vsd_isin, vsd.vsd_on
              FROM vsd, tfv, TENFORE_EXCHANGE_MAP
             WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
                   AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
                   AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id);