sql如何在不同的列中使用多个案例

时间:2018-11-22 07:52:06

标签: mysql sql

SELECT 
description_arab, 
CTS_TRX_TYPE, 
comp_code,
branch_code,
currency_code,
gl_code,
cif_sub_no,
cts_trs_no,
trans_date,
value_date,
fc_amount,
cv_amount,description,case when fc_amount is null then '0' else fc_amount end end as fc  , 
case when CTS_TRX_TYPE is null then 'JV' else CTS_TRX_TYPE end end as TRX_TYPE,
 CASE WHEN CURRENCY_CODE = 368 THEN 'IQD' ELSE CASE WHEN CURRENCY_CODE = 840 THEN 'USD' END  END AS CURRENCY 
 FROM dof WHERE to_date(trans_date,'DD/MM/YYYY')  between   to_date('24/09/18,'DD/MM/YYYY') AND 
 TO_DATE('21/11/18'     ,'DD/MM/YYYY') and currency_code IS NOT NULL
  order by   cts_trs_no

1 个答案:

答案 0 :(得分:0)

是的,您可以在表中的多个列上使用多个case语句。 但是从您的查询中我可以看到多余的END语句,因此只需将它们从查询中删除并更新最后一个案例即可。 不确定这就是您想要的-

SELECT description_arab, 
   cts_trx_type, 
   comp_code, 
   branch_code, 
   currency_code, 
   gl_code, 
   cif_sub_no, 
   cts_trs_no, 
   trans_date, 
   value_date, 
   fc_amount, 
   cv_amount, 
   description, 
   CASE 
          WHEN fc_amount IS NULL THEN '0' 
          ELSE fc_amount 
   END AS fc , 
   CASE 
          WHEN cts_trx_type IS NULL THEN 'JV' 
          ELSE cts_trx_type 
   END AS trx_type, 
   CASE 
       WHEN currency_code = 368 THEN 'IQD' 
       WHEN currency_code = 840 THEN 'USD' 
       ELSE 'INVALID CURRECY' -- added this to check other checks as well
      END as currency 
    FROM   dof 
          WHERE  to_date(trans_date,'DD/MM/YYYY') BETWEEN to_date('24/09/18,'dd/mm/yyyy') AND  TO_DATE('21/11/18'     ,'dd/mm/yyyy') and currency_code IS NOT NULL   order by   cts_trs_no