试图过滤掉空值,它不起作用

时间:2017-11-06 21:50:32

标签: sql oracle

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 个答案:

答案 0 :(得分:3)

Select ......
Where not( attribute1 is null
And   Attribute2 is null
And   Attribute3 is null)

答案 1 :(得分:1)

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

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

您可能还要具体了解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

答案 2 :(得分:0)

由于代码的大小,我将此作为另一个答案发布,而不仅仅是对您的问题的评论。如果切换到ANSI连接语法,并且检查not null:

,它会使您的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'