【问题标题】:Replace NULL with the values将 NULL 替换为值
【发布时间】:2017-08-09 10:00:24
【问题描述】:

我们有如下需求。

有 6 个 item_class - A,B,C,D,A1A,A1B 应该出现在所有记录中。

记录:

Item_class Rev_id
A            1
B            1
C            2
D            1
Null         1
Null         2

我们需要如下表所示显示记录,

item_class     Rev_id
A               1
B               1
C               2
D               1
A1A             1
A1B             2

所以所有的 item_class(A,B,C,D,A1A,A1B) 应该在 item class 为 null 时显示,并且它还会检查 item_clas A,B,C 是否已经存在,然后对于不同的 rev_id 它将显示剩余的一个(即 D、A1A、A1B),但应涵盖所有 item_class。

我们如何编写查询来获取相似的记录?

【问题讨论】:

  • 你应该先展示你尝试过的东西,请阅读How to create a Minimal, Complete, and Verifiable example
  • 我不遵循您将A1AA1B 分配给那些NULL 插槽的逻辑。如果所有记录的Rev_id 都相同,我可以看到一种方法,但如果没有一个共同的组,恐怕你的问题对我来说没有多大意义。
  • 我们尝试过使用case语句但没有成功,当item_class不为null且rev_id不为null时选择case,然后当item_class为null且rev_id不为null时选择item_class? end item_class, rev_id from test_class where rev_id = 1;这里是?,需要检查 item_class A 是否已经可用于 rev_id 1 然后显示 item_class B,如果 item_class B 可用于 rev_id 1 然后显示 item_class C 用于 rev_id 等。我们希望以这种方式查询
  • rev_id 值从何而来? item_class 来自哪里?什么决定了结果集的顺序?如果您想得到答案,您需要正确解释您的要求。
  • 请不要在 cmets 中提供详细信息,而是用相关详细信息更新问题。

标签: sql oracle oracle11g


【解决方案1】:

通过使用外连接解决了这个问题。请在下面找到,

select b.item_class,
       a.rev_id
from   cct_test a 
       right outer join cct_item_details b
       on a.item_class = b.item_class and a.rev_id = 1;

输出:

item_class | rev_id
A          |  1
C          |  1
A1B        |  null
A1A        |  null
D          |  null     
B          |  null

【讨论】:

    猜你喜欢
    • 2020-02-18
    • 2021-04-04
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2018-08-22
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多