简体   繁体   English

需要在一列中查找值,然后从另一列返回值

[英]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!) (我无法更改此表的数据结构!)

  • 21 of the columns in this table stand for 'Actual' sizes 此表中的21列代表“实际”尺寸
  • 21 of the columns in this table stand for 'Relative' sizes 此表中的21列代表“相对”尺寸
  • The Actual size columns correspond through Relative size columns via column number. “实际大小”列通过列号对应“相对大小”列。
    (ex : ActualColumn1 corresponds to RelativeColumn1 , ActualColumn2 corresponds to RelativeColumn2 , ActualColumnX to RelativeColumnX up to 21) (例如:ActualColumn1对应于RelativeColumn1,ActualColumn2对应于RelativeColumn2,ActualColumnX到RelativeColumnX多达21)

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.

相关问题 需要一个 SQL 选择语句来返回在一个列中具有相同 id 而在另一列中具有不同值的行 - Need a SQL select statement to return rows that have the same id in one column and distinct value in another column SQL-返回与另一列值匹配的一列中的最小值 - SQL - return the smallest value in one column that matches the value of another column 从属于另一列中的值的一列中查找并显示一个值的出现? - Find and display occurrence of one value from one column which belongs to value in another column? 返回一个列值的结果,但不返回另一条SQL的结果 - Return results for one column value but not another SQL 当另一列的值更改时,返回一列的第一个和最后一个值 - Return the first and last value from one column when value from another column changes 查找一列的重复项,但只有在另一列具有相同值时才返回结果 - Find duplicates of one column but only return results if another column has same value 我需要连接三个表,将结果分组为一列,并显示另一列的最高值 - I need to join three tables, group the results by one column, and display the highest value from another column 返回依赖于另一列中的值的值 - Return values that relies on a value from another column 如何从一列中找到前 3 个返回值中的每一个,从另一列中查找前 3 个返回值? - How to find for each of the top 3 returned value from one column, the 3 top values from another column? SQL-从一列返回所有唯一的值对,在另一列中返回每个值 - SQL - Return all unique pairs of values from one column, for each value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM