如果那么语句改变现有列

时间:2015-09-04 13:54:54

标签: if-statement teradata

我该怎么做?我尝试了一个案例陈述但是有问题让它发挥作用

if Tax_or_Flat = 'tax' then flat = 0

Tax_or_Flat是列标题
flata是列标题

也许

,CASE WHEN Tax_or_Flat = 'tax' THEN Flata = 0 ELSE Tax_or_Flat END

但那不起作用

select 
a.time_amt as Amt_Day
,a.rate_mix as AVG_Day_Rate
,a.psbdats as Poss_Days_Bil_Ins
,a.acct as Bill_to_Amount
,a.LGCY_AUTH_AMT_TXT as Auth_Amt
      ,case when Auth_Amt  LIKE '%TAX%'  then 'tax'
       when Auth_Amt  LIKE '%DAY%'  then 'flat'
            else 0
            end as Tax_or_Flat
     ,((Amt_Day - AVG_Day_Rate ) * Poss_Days_Bil_Ins) as Tax
       ,((Amt_Day * Poss_Days_Bil_Ins) - Bill_to_Amount) aS Flat


from JTable.Loan a

where a.rate_mix = 5

2 个答案:

答案 0 :(得分:1)

UPDATE table_name
SET flata = CASE WHEN Tax_or_Flat = 'tax' THEN 0 ELSE Tax_or_Flat END

当Tax_or_Flat =' tax'时,这会将flata的值更改为0,并且在所有其他情况下,它会在Tax_or_Flat列中为其提供值。

检查Teradata的语法,但这是您尝试的基本概念。

**编辑**

如果这只是您尝试执行的SELECT,而不是更改任何实际的表数据,则可以执行嵌套子查询:

select *,case when Tax_or_Flat = 'tax' THEN 0 ELSE Tax_or_Flat END as flata
from (
select 
a.time_amt as Amt_Day
,a.rate_mix as AVG_Day_Rate
,a.psbdats as Poss_Days_Bil_Ins
,a.acct as Bill_to_Amount
,a.LGCY_AUTH_AMT_TXT as Auth_Amt
      ,case when Auth_Amt  LIKE '%TAX%'  then 'tax'
       when Auth_Amt  LIKE '%DAY%'  then 'flat'
            else 0
            end as Tax_or_Flat
     ,((Amt_Day - AVG_Day_Rate ) * Poss_Days_Bil_Ins) as Tax
       ,((Amt_Day * Poss_Days_Bil_Ins) - Bill_to_Amount) aS Flat


from JTable.Loan a

where a.rate_mix = 5
) a

或重写查询中的逻辑:

select 
a.time_amt as Amt_Day
,a.rate_mix as AVG_Day_Rate
,a.psbdats as Poss_Days_Bil_Ins
,a.acct as Bill_to_Amount
,a.LGCY_AUTH_AMT_TXT as Auth_Amt
,case when Auth_Amt  LIKE '%TAX%'  then 'tax'
      when Auth_Amt  LIKE '%DAY%'  then 'flat'
      else 0
end as Tax_or_Flat
,((Amt_Day - AVG_Day_Rate ) * Poss_Days_Bil_Ins) as Tax
,((Amt_Day * Poss_Days_Bil_Ins) - Bill_to_Amount) aS Flat
,case when (case when Auth_Amt  LIKE '%TAX%'  then 'tax'
            when Auth_Amt  LIKE '%DAY%'  then 'flat'
            else 0 end) = 'tax' 
      then 0 
      else (case when Auth_Amt LIKE 'TAX%'
                 then 'tax'
                when Auth_Amt  LIKE '%DAY%'     
                then 'flat'
                else 0
            end)
  end AS flata

from JTable.Loan a

where a.rate_mix = 5

答案 1 :(得分:0)

基于您的SELECT,您似乎想要这样的东西:

select 
a.time_amt as Amt_Day
,a.rate_mix as AVG_Day_Rate
,a.psbdats as Poss_Days_Bil_Ins
,a.acct as Bill_to_Amount
,a.LGCY_AUTH_AMT_TXT as Auth_Amt
  ,case when Auth_Amt  LIKE '%TAX%'  then 'tax'
   when Auth_Amt  LIKE '%DAY%'  then 'flat'
        else '0'
        end as Tax_or_Flat
  ,case when Tax_or_Flat = 'tax' 
        then ((Amt_Day - AVG_Day_Rate ) * Poss_Days_Bil_Ins) 
        else 0 
   end as Tax
  ,case when Tax_or_Flat = 'flat' 
        THEN ((Amt_Day * Poss_Days_Bil_Ins) - Bill_to_Amount) 
        else 0 
   end AS Flat    

from JTable.Loan a

where a.rate_mix = 5