[英]Need to find value in one column and then return value from another column
Here is my problem. 这是我的问题。 I have a Microsoft SQL server database table with a bunch of columns in it.
我有一个Microsoft SQL服务器数据库表,其中包含一堆列。 (I cannot change the data structure of this table!)
(我无法更改此表的数据结构!)
Based on the inputted 'Actual' value I need to return the related 'Relative' value. 根据输入的'Actual'值,我需要返回相关的'Relative'值。
I have managed to do part of what I need with the following case statement, but case statements have a maximum of 10 conditions and therefore won't be able to cover all 21 columns in my table. 我已经设法通过以下case语句完成了我需要的部分工作,但case语句最多有10个条件,因此无法覆盖表中的所有21列。 What would be the best way to approach this issue?
解决这个问题的最佳方法是什么? My goal would be to put this into a select statement so that I could select the Relative Size and return it in my query.
我的目标是将它放入一个select语句,以便我可以选择相对大小并在我的查询中返回它。
Example of what I did with the case statement: 我用case语句做的例子:
SELECT
T.AValue
CASE WHEN (T.ActualColumn1 = T.AValue) then T.RelativeColumn1 ELSE
CASE WHEN (T.ActualColumn2 = T.AValue) THEN T.RelativeColumn2 ELSE
CASE WHEN (T.ActualColumn3 = T.AValue) THEN T.RelativeColumn3 ELSE
CASE WHEN (T.ActualColumn4 = T.AValue) THEN T.RelativeColumn4 ELSE
NULL
END
END
END
END AS RValue
FROM T
Thank you for your help! 谢谢您的帮助!
If you change your case statement to a searched case you can have more then 10 when clauses. 如果将case语句更改为已搜索的案例,则可以包含10个以上的子句。
select T.AValue,
case T.AValue
when T.ActualColumn1 then T.RelativeColumn1
when T.ActualColumn2 then T.RelativeColumn2
when T.ActualColumn3 then T.RelativeColumn3
when T.ActualColumn4 then T.RelativeColumn4
end as RValue
from T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.