如何根据Oracle中的条件选择列值

时间:2018-10-03 16:54:56

标签: sql database oracle

我有一张要用来生成报告的表格。我可以传递参数值来调用另一个使用查询填充数据的报表。应该根据我们传递的参数更改数据,并根据参数值选择列值

这是我的样本输入和基于参数的预期输出。

输入表

id_val      name    flag_1   f1_val     flag_2   f2_val     flag_3   f3_val
1           USD      1          aa         0        gg            1    mm
2           CAD      0          bb         0        hh            0    nn
3           INR      1          cc         1        ii            0    oo
4           JPY      0          dd         1        jj            1    pp
5           EUR      1          ee         1        kk            1    qq
6           AUD      1          ff         1        ll            0    rr

基于参数值的输出

如果参数= Flag1

name    flag   f_val
USD     1       aa
INR     1       cc
EUR     1       ee
AUD     1       ff

如果参数= Flag2

name    flag  f_val
INR     1      ii
JPY     1      jj
EUR     1      kk
AUD     1      ll

如果参数= Flag3

name    flag   f_val
USD     1       mm
JPY     1       pp
EUR     1       qq

3 个答案:

答案 0 :(得分:1)

我会在WHERE子句中使用布尔逻辑:

SELECT t.name, 1 AS Flag, 
       (CASE WHEN Paramerter = 'Flag1' THEN flag_1
             WHEN Paramerter = 'Flag2' THEN flag_2
             WHEN Paramerter = 'Flag3' THEN flag_3 
        END) AS f_val
FROM table t
WHERE (Paramerter = 'Flag1' AND flag_1 = 1) OR
      (Paramerter = 'Flag2' AND flag_2 = 1) OR
      (Paramerter = 'Flag3' AND flag_3 = 1);

答案 1 :(得分:1)

您正在寻找枢纽,我将使用UNION ALL并为每个标志添加一个grp

 SELECT t1.* FROM (
    SELECT name,flag_1 flag,'Flag1' grp
    FROM T
    UNION ALL
    SELECT name,flag_2,'Flag2' grp
    FROM T
    UNION ALL
    SELECT name,flag_3,'Flag3' grp
    FROM T
) t1
WHERE t1.grp = ? and t1.flag > 0

注意

?是您的参数输入,它可以代替'Flag1''Flag2' ...


这是一个样本

CREATE TABLE T(
   id_val int,
   name varchar(50),
   flag_1 int,
   flag_2 int,
   flag_3 int
);


insert into t values (1 ,'USD',1,0,1);
insert into t values (2 ,'CAD',0,0,0);
insert into t values (3 ,'INR',1,1,0);
insert into t values (4 ,'JPY',0,1,1);
insert into t values (5 ,'EUR',1,1,1);
insert into t values (6 ,'AUD',1,1,0);

查询1

SELECT t1.NAME,t1.FLAG
FROM (
    SELECT name,flag_1 flag,'Flag1' grp
    FROM T
    UNION ALL
    SELECT name,flag_2,'Flag2' grp
    FROM T
    UNION ALL
    SELECT name,flag_3,'Flag3' grp
    FROM T
) t1
WHERE t1.grp = 'Flag3' and t1.flag > 0

Results

| NAME | FLAG |
|------|------|
|  USD |    1 |
|  JPY |    1 |
|  EUR |    1 |

答案 2 :(得分:0)

您可以轻松,干净地使用解码:

select name,1 as flag,
            decode('&Parameter','Flag1',F1_val
                               ,'Flag2',F2_val
                               ,'Flag3',F3_val) as f_val
  from tab
 where decode('&Parameter','Flag1',Flag_1 
                          ,'Flag2',Flag_2
                          ,'Flag3',Flag_3) = 1;

假设'&Parameter'是一个字符串类型替换变量,可以为其传递值'Flag1''Flag2''Flag3'

-- if '&Parameter' = 'Flag1'
NAME    FLAG    F_VAL
USD      1      aa
INR      1      cc
EUR      1      ee
AUD      1      ff

-- if '&Parameter' = 'Flag2'
NAME    FLAG    F_VAL
INR     1       ii
JPY     1       jj
EUR     1       kk
AUD     1       ll

-- if '&Parameter' = 'Flag3'
NAME    FLAG    F_VAL
USD     1       mm
JPY     1       pp
EUR     1       qq

Rextester Demo