【问题标题】:case statement select案例陈述选择
【发布时间】:2019-03-28 01:44:13
【问题描述】:
|AMNT1   |  AMNT2 |  Rank
__________________________
|  01    |      05 |     rank1   
|  05    |     10 |  rank2   
| 10     |     15 |  rank3  
___________________________

我需要case 语句,当值介于两者之间时提供输出rank1,rank2,rank3

1 and 5rank1、5 and 10rank2 等

我有一个包含 deptid 和金额的金额表

需要的输出是 deptid 数量 rank 需要从 rank 表中提取 rank

如何编写select 查询并将其放入case 语句?

它应该是动态的(我的意思是表值可以改变,但我的case 语句应该读取最新值并提供输出)

【问题讨论】:

  • 所以这是一个查找表,您想查询一些其他数据并根据与查找比较的一些值添加该排名标志?一个你想如何使用它的例子会很有帮助。
  • 我不太了解问题的“动态”部分。这是什么意思?
  • 如果你的数量正好是 5,那应该是排名 1 还是 2,因为该值在两个范围内?您可能真的不希望它们像那样重叠。 1 或 15 的数量呢?或任何范围之外的任何东西,例如0 还是 16?

标签: sql oracle select case


【解决方案1】:

您不需要case。您可以使用相关子查询:

select (select rt.rank from ranktable rt where a.amount >= rt.amnt1 and a.amount < rt.amnt2) as ranking
from amount a;

您也可以使用join 来执行此操作。

【讨论】:

  • 谢谢 .. 但不知何故它对我不起作用:( ORA-01427: 单行子查询返回多行 01427. 00000 - “单行子查询返回多行” *原因:*行动:
  • @Gordon Linoff:t 是什么?
  • @Trevor 。 . . t 是被查询的表名。
  • @PraphulViswan 。 . .那么,你打算如何在两个都匹配不等式条件的行之间进行选择?
  • @GordonLinoff: 但原表中没有 t.amnt。
【解决方案2】:

您是否在寻找以下案例陈述:

select * from (select case when input_amt>AMNT1 and input_amt<AMNT2 THEN rank end  RANK
from ranktable) rnk
where rnk.RANK is not null

【讨论】:

  • 输入金额需要从另一个表中读取。我没有他们之间的加入标准。我可以在 PL/SQL 中做到这一点,我想知道 SQL 语句是否可以做到这一点。 Amount_Table 和 Rank_Table 没有加入标准 - 这就是挑战。
【解决方案3】:

这是一个联接版本,在 CTE 中有示例数据:

with rankings (amnt1, amnt2, rank) as (
            select 1, 5, 'rank1' from dual
  union all select 5, 10, 'rank2' from dual
  union all select 10, 15, 'rank3' from dual
),
amount (deptid, amount) as (
            select 1, 0 from dual
  union all select 2, 1 from dual
  union all select 3, 4 from dual
  union all select 4, 5 from dual
  union all select 5, 6 from dual
  union all select 6, 9 from dual
  union all select 7, 10 from dual
  union all select 8, 11 from dual
  union all select 9, 15 from dual
  union all select 10, 16 from dual
)
-- actual query
select a.deptid, a.amount, r.rank
from amount a
left join rankings r on a.amount >= r.amnt1 and a.amount < r.amnt2;

这与 Gordon 的子查询方法得到相同的结果:

    DEPTID     AMOUNT RANK 
---------- ---------- -----
         1          0      
         2          1 rank1
         3          4 rank1
         4          5 rank2
         5          6 rank2
         6          9 rank2
         7         10 rank3
         8         11 rank3
         9         15      
        10         16      

如您所见,使用这些比较条件意味着 ui 不匹配 15。您可以更改它们:

select a.deptid, a.amount, r.rank
from amount a
left join rankings r on a.amount > r.amnt1 and a.amount <= r.amnt2;

    DEPTID     AMOUNT RANK 
---------- ---------- -----
         1          0      
         2          1      
         3          4 rank1
         4          5 rank1
         5          6 rank2
         6          9 rank2
         7         10 rank2
         8         11 rank3
         9         15 rank3
        10         16      

但现在 1 不匹配。如果您尝试将两者都与 between 等效:

select a.deptid, a.amount, r.rank
from amount a
left join rankings r on a.amount >= r.amnt1 and a.amount <= r.amnt2;

    DEPTID     AMOUNT RANK 
---------- ---------- -----
         1          0      
         2          1 rank1
         3          4 rank1
         4          5 rank1
         4          5 rank2
         5          6 rank2
         6          9 rank2
         7         10 rank2
         7         10 rank3
         8         11 rank3
         9         15 rank3
        10         16      

由于行重叠,您现在会获得多个结果。例如,当有不止一场比赛时,您可以通过保持“较高”排名来摆脱它们:

select a.deptid, a.amount,
  max(r.rank) keep (dense_rank last order by r.amnt1) as rank
from amount a
left join rankings r on a.amount >= r.amnt1 and a.amount <= r.amnt2
group by a.deptid, a.amount;

    DEPTID     AMOUNT RANK 
---------- ---------- -----
         1          0      
         2          1 rank1
         3          4 rank1
         4          5 rank2
         5          6 rank2
         6          9 rank2
         7         10 rank3
         8         11 rank3
         9         15 rank3
        10         16      

但是,实际上,您不应该有重叠;或者您需要定义哪个结果是正确的。 (如果您的数据总是匹配某些东西,您甚至可能根本不需要/想要一个下限;但这也是一个稍微不同的查询。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2010-10-30
    • 2016-05-13
    • 2016-02-21
    • 1970-01-01
    • 2012-09-08
    • 2013-10-14
    相关资源
    最近更新 更多