【问题标题】:Oracle Select Query Populate default value as NA in the rowOracle Select Query Populate 默认值为 NA 在行中
【发布时间】:2021-05-12 20:11:32
【问题描述】:

假设查询是 从 test_table 中选择 con, a, b, c, d, e where con in (1,2,3,4,5);

现在对于 1、2、4、5,表格有数据,但对于 3,没有数据。

所以输出将如下所示。

con     a      b     c     d     e
1       s1     s2    s3    s4   s5
2       s3     s5    s6    s7   s2
4       s3     s7    s1    s5   s8
5       s4     s8    s4    s7   s8



 but how to get the result as follows

con     a      b     c     d     e
1       s1     s2    s3    s4   s5
2       s3     s5    s6    s7   s2
3       N/A    N/A   N/A   N/A  N/A
4       s3     s7    s1    s5   s8
5       s4     s8    s4    s7   s8

需要帮助。尝试了不同的组合,但无法做到这一点。

【问题讨论】:

    标签: sql database oracle


    【解决方案1】:
    with   helper (con) as (select level from dual connect by level <= 5)
    select con, a, b, c, d, e
    from   helper left outer join test_table using (con)
    ;
    

    编辑

    一种更通用的方法(也可以适用于字符串而不是IN 列表中的数字):

    with helper (con) as (select column_value from table(sys.odcinumberlist(1,2,3,4,5)))
    ........
    

    【讨论】:

    • 这个con可以是任何条件,也可以是任何字符串,我只是添加了一个示例以便理解我的查询。
    • @DipakChatterjee - 我不明白你的评论。无论您在IN 列表中有什么值,您都可以从这些值创建一个“帮助表”。而不是使用connect by 查询,您可以(select column_value from sys.odcivarchar2list('ab1', 'cd2', ....)(如果列表是字符串列表)等。关键思想是helper 视图,没关系如何您使用与IN 列表中相同的值填充它。
    • 明白了,但是左外连接会显示空我猜,如何在那里显示一些文本?就像我的问题一样。我想要在每个单元格中不适用,我正在尝试这样做。
    • 完成,使用的Case函数不知道,因为这里的MYSQL也可以使用CASE函数。
    • @DipakChatterjee - 现在我明白你的评论了。但是问题:您可以在输出中的任何地方使用 N/A 而不是 null(空字符串),或者仅在由于 test_table 中缺少 con 值而创建的完整行中(但保持 null 不变) test_table 中的内容)。你需要哪一个?两种方法都可以轻松完成,但两种情况的解决方案略有不同。
    猜你喜欢
    • 2019-03-19
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2016-10-04
    • 2012-08-30
    • 2016-10-31
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多