首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用高效的查询替换许多"IF's“

如何用高效的查询替换许多"IF's“
EN

Stack Overflow用户
提问于 2020-04-20 13:19:00
回答 2查看 43关注 0票数 0

它要求获得工资,但从一些状态实体(健康和afp)打折。例如,这些实体有不同的折扣,而ppl将有不同的Afp和Health,所有这些都是混合的。

  • afp 1 5% dsct
  • afp 2 10%
  • afp 3 15% dsct

  • 健康17%dsct
  • 健康2 12% dsct
  • 健康3 18% dsct

我怎样才能拿到薪水,却又不多呢?

代码语言:javascript
复制
IF (afp=1)
  IF (health=1)
  IF (health=2)
  IF (health=3)
IF (afp=2)
  IF (health=1)
  IF (health=2)
  IF (health=3)

以此类推,有什么办法可以避免这样的情况吗?希望你们能帮我,thnks

EN

回答 2

Stack Overflow用户

发布于 2020-04-20 13:22:06

您可以使用case表达式:

代码语言:javascript
复制
select (case when afp = 1 and health = 1 then 0.05
             when afp = 1 and health = 2 then 0.10
             when afp = 1 and health = 3 then 0.15
             when afp = 2 and health = 1 then 0.07
             when afp = 2 and health = 2 then 0.12
             when afp = 2 and health = 3 then 0.18
        end) as discount

或者引用表,您可以动态构建:

代码语言:javascript
复制
select t.*, v.discount
from t left join
     (values (1, 1, 0.05), (1, 2, 0.10), . . .
     ) v(afp, health, discount)
     on t.afp = v.afp and t.health = v.health
票数 1
EN

Stack Overflow用户

发布于 2020-04-20 13:38:38

另一个选择是choose()

示例

代码语言:javascript
复制
Select Discount = choose(aft,.05,.10,.15) + choose(health,.02,.02,.03)
 From  YourTable
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61323478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档