【问题标题】:Can I do case when the last 3 string equal aaa then do...?我可以做最后 3 个字符串等于 aaa 的情况,然后做...吗?
【发布时间】:2014-09-23 20:49:28
【问题描述】:

我有一串数据,我想看看我是否可以做一个 case when 语句,如果它以某些文本结尾,然后从不同的临时表返回值。是否有可能做到这一点? 即,当 prodwarehouse 以“001”结尾时,然后从表 #a else 0 end 中提取现有数量。 prodwarehouse 以 '009' 结尾的情况,然后从表 #b 中提取现有数量,否则 0 结束。

表 #a 和 #b 具有这些值:productwarehouse - on hand qty

【问题讨论】:

  • 我刚刚发现我仍然可以使用 case when 执行 %string%,我会尝试的。但是我们可以根据条件从那些表#a 或#b 中提取某些值吗?
  • like 语句并不总是最快的。 Right(field,3) 可以在某些数据库中使用...您使用的是什么数据库?

标签: sql case


【解决方案1】:

不太了解涉及多少表或关系,但您可以使用带有子选择的 case 语句。

select 
  case when prodwarehouse like '%001' then (select onHandQty from tableA a where yt.id = a.id) 
       when prodwarehouse like '%009' then (select onHandQty from tableB b where yt.id = b.id)
       else 0
  end as 'Qty'
from yourTable yt

或者您可以只加入表格,这样您就不需要进行子选择。

select 
  case when a.prodwarehouse like '%001' then b.qty
       when a.prodwarehouse like '%009' then c.qty
       else 0
  end as 'Qty'
from tablea a
join table b on...
join table c on...

【讨论】:

  • @user3508386 如果对您有用,请将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 2010-12-17
  • 2017-09-08
相关资源
最近更新 更多