简体   繁体   English

在 SQL 查询中使用另一个 CASE 表达式中的列值

[英]Use a CASE expression Column value in another CASE expression in SQL Query

I am trying to user one column obtained from the CASE expression to SET another Column Value.我正在尝试使用从 CASE 表达式获得的一列来设置另一列值。

EG I have a table user with multiple column. EG 我有一个多列的表用户。 First, I need to set the status of a project based on the values of some columns in the table.首先,我需要根据表中某些列的值设置项目的状态。 I am able to make this part work.我能够使这部分工作。 The second part is, based on the status value, I need to get the user name(s) from multiple columns.第二部分是,根据状态值,我需要从多列中获取用户名。 When I do that, am getting an error当我这样做时,出现错误

Invalid Column 'Status'列“状态”无效

A sample query follows:示例查询如下:

  SELECT
  STATUS = CASE WHEN ISNULL(USER1,'') = '1' THEN 'Approve'
           WHEN ISNULL(USER2,'') = '2' THEN 'Reject'
           WHEN ISNULL(USER3,'') = '3' THEN 'Pending Decision'
           ELSE 'error' END,
 USERNAME = CASE WHEN ISNULL(STATUS,'') = 'Approve' THEN (select username1 from <table> where userid = <userid>)
           WHEN ISNULL(STATUS,'') = 'Reject' THEN  (select username2 from <table> where userid = <userid>)
           WHEN ISNULL(STATUS,'') = 'Pending Decision' THEN  (select username3 from <table> where userid = <userid>)
           ELSE 'error' END

           from <table> where userid = <userid>

This works, because @status will be processed before username这是可行的,因为 @status 将在用户名之前被处理

And you have to use user defined variables, you can't use columnnames for that而且您必须使用用户定义的变量,不能为此使用列名

  SELECT
  @status := (CASE WHEN COALESCE(USER1,'') = '1' THEN 'Approve'
           WHEN COALESCE(USER2,'') = '2' THEN 'Reject'
           WHEN COALESCE(USER3,'') = '3' THEN 'Pending Decision'
           ELSE 'error' END)  `STATUS` 
           ,
  CASE WHEN COALESCE(@status,'') = 'Approve' THEN (select username1 from table1 where userid = 20)
          WHEN COALESCE(@status,'') = 'Reject' THEN  (select username2 from table1 where userid = 20)
           WHEN COALESCE(@status,'') = 'Pending Decision' THEN  (select username3 from table1 where userid = 20)
           ELSE 'error' END USERNAME 

           from table1 where userid = 20

You can use a "table expression" to pre-compute a column.您可以使用“表表达式”来预先计算列。 For example:例如:

select
  *,
  -- use the pre-computed STATUS column now
from (
  SELECT *,
    <CASE-expression> as status
  from <table> where userid = <userid>
) x

You can't reuse an expression defined in the select clause in the same clause.您不能在同一子句中重复使用select子句中定义的表达式。

I don't really see why you need the value returned by the first expression to compute the second value, since the logic is basically the same.我真的不明白为什么你需要第一个表达式返回的值来计算第二个值,因为逻辑基本相同。

You can just do:你可以这样做:

select
    case 
        when user1 = 1 then then 'Approve'
        when user2 = 2 then 'Reject'
        when user3 = 3 then 'Pending Decision'
        else 'error' 
    end status,
    case 
        when user1 = 1 then (select username1 from <table> where userid = <userid>)
        when user2 = 2 then (select username2 from <table> where userid = <userid>)
        when user3 = 3 then (select username3 from <table> where userid = <userid>)
    end username
from <table> where userid = <userid>

Notes笔记

  • isnull() is unnecessary, since the alternative value is not trapped anywhere in the case statement isnull()是不必要的,因为替代值不会被困在case语句中的任何地方

  • this treats user1 as a number, because it looks like it - so I unquoted the test values in the case statement.这将user1视为一个数字,因为它看起来像 - 所以我在case语句中取消引用测试值。 If it's actually a string, you can revert this and add the single quotes.如果它实际上是一个字符串,您可以将其还原并添加单引号。

  • I am quite suspicious about the logic of the subqueries;我对子查询的逻辑非常怀疑; there might be ways to simplify that, if you were to provide more details on what they are intented to do, along with sample data and desired results.如果您要提供有关他们打算做什么的更多详细信息,以及示例数据和期望的结果,可能有一些方法可以简化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM