Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OracleSQL使用if基于多列强制转换新变量_Sql_Oracle - Fatal编程技术网

OracleSQL使用if基于多列强制转换新变量

OracleSQL使用if基于多列强制转换新变量,sql,oracle,Sql,Oracle,我有一个包含四个变量的表a、x1、x2、x3。保证每个记录的x1alter table tbl添加始终生成的y编号 2作为(当 如果这些是表中的列,则您可以始终使用虚拟列,如下所示: SQL> -- Creating the table SQL> create table tbl 2 (a number, 3 x1 number, 4 x2 number, 5 x3 number); Table created. SQL> -- inserting

我有一个包含四个变量的表
a、x1、x2、x3
。保证每个记录的
x1
。我想创建一个新变量
y
where
y=1
if
a
case when
case when您可以使用
case

(case when a < x1 then 1
      when a < x2 then 2
      else 3
 end) as y
(a
您可以使用
案例

(case when a < x1 then 1
      when a < x2 then 2
      else 3
 end) as y
(a
如果这些是表中的列,则您可以始终使用
虚拟列
,如下所示:

SQL> -- Creating the table
SQL> create table tbl
  2  (a number,
  3  x1 number,
  4  x2 number,
  5  x3 number);

Table created.

SQL> -- inserting some data
SQL> insert into tbl(a,x1,x2,x3) values(0,1,2,3);

1 row created.

SQL> -- this is your solution
SQL> alter table tbl add y number generated always
  2  as (case when a<x1 then 1 when a<x2 then 2 when a<x3 then 3 end) VIRTUAL;

Table altered. 

SQL> -- table data now
SQL> select * from tbl;

         A         X1         X2         X3          Y
---------- ---------- ---------- ---------- ----------
         0          1          2          3          1

SQL>
SQL>——创建表
SQL>创建表tbl
2(一个数字,
3 x1编号,
4 x2编号,
5×3号);
表已创建。
SQL>--插入一些数据
SQL>插入tbl(a,x1,x2,x3)值(0,1,2,3);
创建了1行。
SQL>--这是您的解决方案
SQL>alter table tbl添加始终生成的y编号
2作为(当

如果这些是表中的列,则您可以始终使用
虚拟列
,如下所示:

SQL> -- Creating the table
SQL> create table tbl
  2  (a number,
  3  x1 number,
  4  x2 number,
  5  x3 number);

Table created.

SQL> -- inserting some data
SQL> insert into tbl(a,x1,x2,x3) values(0,1,2,3);

1 row created.

SQL> -- this is your solution
SQL> alter table tbl add y number generated always
  2  as (case when a<x1 then 1 when a<x2 then 2 when a<x3 then 3 end) VIRTUAL;

Table altered. 

SQL> -- table data now
SQL> select * from tbl;

         A         X1         X2         X3          Y
---------- ---------- ---------- ---------- ----------
         0          1          2          3          1

SQL>
SQL>——创建表
SQL>创建表tbl
2(一个数字,
3 x1编号,
4 x2编号,
5×3号);
表已创建。
SQL>--插入一些数据
SQL>插入tbl(a,x1,x2,x3)值(0,1,2,3);
创建了1行。
SQL>--这是您的解决方案
SQL>alter table tbl添加始终生成的y编号
2作为(当

因此它将是
从…
中选择(案例…)作为y,正确吗?@SAFEX…是。因此它将是
从…
中选择(案例…)作为y,正确吗?@SAFEX…是。