【问题标题】:Trying to filter out nulls and it isn't working试图过滤掉空值但它不起作用
【发布时间】:2017-11-09 02:21:45
【问题描述】:

这里是 Oracle SQL 新手。

我有三列包含很多空值。我正在尝试过滤掉所有三列都具有空值的所有行。

但是,它仍然为下面的合并公式返回空值。有任何想法吗?

select name, city, attribute1, attribute2, attribute3
from table1
where coalesce(attribute1,attribute2,attribute3) is not null

我尝试使用 CONCAT 公式做同样的事情,但得到了相同的结果。当我在 CONCAT 公式中使用 nvl(attribute1,'None') 时,我能够让它工作,但我希望有更简单的方法。

编辑:我仍然没有得到正确的结果。我在下面发布完整的查询。也许我的语法还有另一个错误。

select 
gjb.name "Batch",
gjl.attribute_category,
gjh.name,
gjh.created_by,
gjh.period_name,
gcc.segment1,
gcc.segment2,
gcc.segment3,
gcc.segment4,
gcc.segment5,
gcc.segment6,
gjl.attribute1,
gjl.attribute2,
gjl.attribute3,
gjl.attribute4,
gjl.attribute5,
gjl.attribute6,
gjl.attribute7,
gjl.attribute8,
gjl.attribute9,
gjl.attribute10,
nvl(gjl.entered_dr,0)-nvl(entered_cr,0) "Amt"

from gl_je_lines gjl, gl_je_headers gjh, gl_je_batches gjb, gl_code_combinations gcc
where not (gjl.attribute1 is null 
and gjl.attribute2 is null
and gjl.attribute3 is null
and gjl.attribute4 is null
and gjl.attribute5 is null
and gjl.attribute6 is null
and gjl.attribute7 is null
and gjl.attribute8 is null
and gjl.attribute9 is null
and gjl.attribute10 is null)
and gjl.je_header_id = gjh.je_header_id
and gjh.je_batch_id = gjb.je_batch_id
and gcc.code_combination_id = gjl.code_combination_id
and gjb.je_source in ('Manual','Spreadsheet')
and gjh.period_name in (:Period)
and upper(gjb.name) not like '%BEGBAL%'
and gjl.status = 'P'

【问题讨论】:

  • 这应该行得通,你真的用这个语句得到所有 3 个都为 null 的结果吗?
  • @Ab Bennet 有正确的想法。通过使用合并,您正在破坏优化器使用约束和索引的能力。至于为什么您的代码不起作用,请提供一个小样本数据集来说明此问题。
  • 我和@Hogan 在一起——这应该可以。您确定 JE Lines DFF 列中没有空格吗?各栏的内容因期刊来源而异 - 电子表格几乎可以导入任何内容
  • @BrianLeach 我编辑了我的原始帖子。我仍然收到错误消息。有任何想法吗?感谢您的帮助。
  • 是问题所在,因为您已将过滤条件扩展到属性 4 之后。这是想要的结果吗?上面的 where not (...) 只会返回至少填写了一个属性的行。如果填写了属性 4 但 1 到 3 不是该行,仍将返回该行。

标签: sql oracle


【解决方案1】:
Select ......
Where not( attribute1 is null
And   Attribute2 is null
And   Attribute3 is null)

【讨论】:

  • 似乎是最简单的解决方案。
  • ??混合 not( ... is null and ... is not null) 认为 2 not's too many
  • 是的,我的错,忘了修复它
  • @AbBennett 我仍然没有得到正确的结果。我已经编辑了我的原始帖子以展示我正在做的一切。
  • 我在上面添加了评论。如果仍然无法正常工作,请告诉我。
【解决方案2】:

您的代码正在执行:“过滤掉在三列中的任何一列中具有空值的行”

您似乎想要:“过滤掉所有三列都具有空值的行”。

您不妨具体了解NULL 比较:

select name, city, attribute1, attribute2, attribute3
from table1
where attribute1 is not null or attribute2 is not null or attribute3 is not null;

对于“任意”版本,只需将or 更改为and

【讨论】:

  • 我正在使用 Oracle BI Publisher,OR 语句需要永远在这个环境中运行。任何不使用 OR 语句的想法?
  • @Dan 。 . .这种类型的查询似乎需要全表扫描。你一定有一张很大的桌子。
【解决方案3】:

由于代码的大小,我将其发布为另一个答案,而不仅仅是对您问题的评论。如果您切换到 ANSI 连接语法并检查不为空,它会使您的 SQL 更容易理解:

SELECT gjb.name "Batch"
     , gjl.attribute_category
     , gjh.name
     , gjh.created_by
     , gjh.period_name
     , gcc.segment1
     , gcc.segment2
     , gcc.segment3
     , gcc.segment4
     , gcc.segment5
     , gcc.segment6
     , gjl.attribute1
     , gjl.attribute2
     , gjl.attribute3
     , gjl.attribute4
     , gjl.attribute5
     , gjl.attribute6
     , gjl.attribute7
     , gjl.attribute8
     , gjl.attribute9
     , gjl.attribute10
     , NVL (gjl.entered_dr, 0) - NVL (entered_cr, 0) "Amt"
  FROM gl_je_lines gjl
       INNER JOIN gl_je_headers gjh ON gjl.je_header_id = gjh.je_header_id
       INNER JOIN gl_je_batches gjb ON gjl.je_header_id = gjh.je_header_id
       INNER JOIN gl_code_combinations gcc
           ON gjh.je_batch_id = gjb.je_batch_id
          AND gcc.code_combination_id = gjl.code_combination_id
 WHERE (gjl.attribute1 IS NOT NULL
     OR gjl.attribute2 IS NOT NULL
     OR gjl.attribute3 IS NOT NULL
     OR gjl.attribute4 IS NOT NULL
     OR gjl.attribute5 IS NOT NULL
     OR gjl.attribute6 IS NOT NULL
     OR gjl.attribute7 IS NOT NULL
     OR gjl.attribute8 IS NOT NULL
     OR gjl.attribute9 IS NOT NULL
     OR gjl.attribute10 IS NOT NULL)
   AND gjb.je_source IN ('Manual', 'Spreadsheet')
   AND gjh.period_name IN (:period)
   AND UPPER (gjb.name) NOT LIKE '%BEGBAL%'
   AND gjl.status = 'P'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2013-07-22
    • 1970-01-01
    相关资源
    最近更新 更多