SQL缺少if语句

SQL缺少if语句,sql,if-statement,Sql,If Statement,请帮忙。我的表格中有以下几列:a、b、c、d、e、g、h 我想做一些处理,只得到3列a,price和h select a, if a = b and d = e then g/h as price else if a = d then g/100 as price else h/100 as price if price = 0 then price = 0.1, h from my table 谢谢大家,但我仍然得到“无效数量的参数”。下面是我的SQL 选择TICKTID作为BLO 当名义货

请帮忙。我的表格中有以下几列:a、b、c、d、e、g、h

我想做一些处理,只得到3列a,price和h

select a, if a = b and d = e then g/h as price
else
if a = d then g/100 as price
else
h/100 as price
if price = 0
then
price = 0.1,
h
from my table

谢谢大家,但我仍然得到“无效数量的参数”。下面是我的SQL

选择TICKTID作为BLO

当名义货币=基础货币和溢价货币=期限货币时的情况 然后 四舍五入市场价格/名义价格,9 否则

以价格结束

,术语(货币)


从DBAPP.SD_STAGE

用例,而不是if,它将执行您想要的操作


e、 g.类似于选择a,当a=b和d=e时选择case,然后当a=d时选择g/h else case,然后选择g/100 else h/100 else.1 end else.1 end as price from my_table

SQL中的条件语句语法为

CASE WHEN <condition> THEN a ELSE b END CASE
简化:

select a, 
       case when a = b and d = e and h > 0 and g > 0 then g/h
            when a = d and g > 0 then g/100 
            when h > 0 then h/100
            else 0.1
       end case  as price,
       h
from my table

在某些SQL变体中可能更简单,但为了涵盖所有这些,您可以嵌套一个案例


这个SQL来自哪里?它看起来不太像SQL。那么这里的问题是什么?哪些RDBMS?MySQL?SQL Server?神谕谢谢大家,但我仍然得到“无效数量的参数”。下面是我的SQL选择TICKTID作为BLO,当名义货币=基础货币和溢价货币=期限货币,然后是圆期限市场价格/名义,9 else case当名义货币=溢价货币时,然后是roundBASE市场价格百分比/100,9 else roundTERM市场价格百分比/100,9 else.1 end as PRICE,TERM货币DBAPP.SD_stage我看到的错误是,在我的示例中,我首先结束了内部案例语句,然后用else表示外壳,然后用end表示外壳,这两个end语句正好在一起。它需要是else-end-else-end,您有else-else-end-end可能不是唯一的错误,但让我们修复它,然后看看是否还有其他错误。
select a, 
       case when (case when a = b and d = e and h > 0 then g/h
            when a = d then g/100 
            else  h/100
            end case) = 0 then .1 
       else (case when a = b and d = e then g/h
            when a = d then g/100 
            else  h/100
            end case) 
       end case  as price,
       h
from my table
select a, 
       case when a = b and d = e and h > 0 and g > 0 then g/h
            when a = d and g > 0 then g/100 
            when h > 0 then h/100
            else 0.1
       end case  as price,
       h
from my table
SELECT a, CASE
          WHEN a = b AND d = e THEN CASE WHEN g=0 THEN 0.1 ELSE g/h   END 
          WHEN a = d           THEN CASE WHEN g=0 THEN 0.1 ELSE g/100 END
          ELSE                      CASE WHEN h=0 THEN 0.1 ELSE h/100 END            
          END price, h
FROM  my_table