1

使用 case 语句执行此选择时收到上述错误。我尝试了几种方法,但无法使语法正确。

SELECT m.md_security_id,
   thv.unit_qty,
   thv.contract_value_amt,
   thv.contract_currency_cd,
   (case when
   THV.BORROW_LOAN_TYPE_CD = 'B'
   then select THV.SENDER_ORG_ID as BORROWER_ORG_ID,
           select THV.RECIPIENT_ORG_ID as LENDER_ORG_ID
     end),
   (case when
   THV.BORROW_LOAN_TYPE_CD = 'L'
   THEN select THV.RECIPIENT_ORG_ID as BORROWER_ORG_ID,
            select THV.SENDER_ORG_ID as LENDER_ORG_ID
    end)   
    FROM eqmddev.txn_header_mv thv,
   eqmddev.md_security m,
   eqmddev.security_xref_vw_tbl x
   WHERE     m.isin = x.security_id
   AND ThV.SECURITY_MASTER_ID = X.SECURITY_MASTER_ID
   AND M.EXCHANGE_CD = X.EXCHANGE_CD
4

1 回答 1

1

你需要像这样重写你的查询:

SELECT m.md_security_id,
   thv.unit_qty,
   thv.contract_value_amt,
   thv.contract_currency_cd,
   case 
    when THV.BORROW_LOAN_TYPE_CD = 'B' then THV.SENDER_ORG_ID 
    when THV.BORROW_LOAN_TYPE_CD = 'L' THEN THV.RECIPIENT_ORG_ID 
   end AS BORROWER_ORG_ID,
   case 
    when THV.BORROW_LOAN_TYPE_CD = 'B' then THV.RECIPIENT_ORG_ID 
    when THV.BORROW_LOAN_TYPE_CD = 'L' THEN THV.SENDER_ORG_ID 
   end AS LENDER_ORG_ID,
FROM eqmddev.txn_header_mv thv,
   eqmddev.md_security m,
   eqmddev.security_xref_vw_tbl x
WHERE     m.isin = x.security_id
   AND ThV.SECURITY_MASTER_ID = X.SECURITY_MASTER_ID
   AND M.EXCHANGE_CD = X.EXCHANGE_CD

基本上,您不能从 CASE 语句中返回多个列。将其视为单个列的条件替换。BORROWER_ORG_ID在上面的示例中,我对别名和LENDER_ORG_ID别名使用了相同的 CASE 条件。

在旁注中,您还应该考虑在您的 CASE 中包含一个 ELSE 语句。否则,如果THV.BORROW_LOAN_TYPE_CD不是,B否则L您将获得 NULL

于 2013-02-28T22:03:28.560 回答