Sql 如何从OracleDB返回非空列
我正在创建一个关于eclipse的birt报告,我需要设置一个日期过滤器。 我在报告中有多个日期字段,其中一些字段为空。 问题是:如何使用正确的日期(非空)作为过滤器的参数?我正在尝试这样做:Sql 如何从OracleDB返回非空列,sql,oracle,birt,Sql,Oracle,Birt,我正在创建一个关于eclipse的birt报告,我需要设置一个日期过滤器。 我在报告中有多个日期字段,其中一些字段为空。 问题是:如何使用正确的日期(非空)作为过滤器的参数?我正在尝试这样做: select case when D___1 is null then D___2 when D___2 is null then D___3 when D___3 is null then D___4 when D___4 is null then D___5 end as data COALESC
select
case
when D___1 is null then
D___2
when D___2 is null then
D___3
when D___3 is null then
D___4
when D___4 is null then
D___5
end as data
COALESCE(D___1, D___2, D___3, D___4, D___5) as DATA
然后这将返回一个有效的日期,我将使用它作为过滤器。但这似乎不起作用。有什么想法吗?
提前感谢。您可以使用该功能。它返回给定列表中的第一个“NOTNULL”值
会是这样的:
select
case
when D___1 is null then
D___2
when D___2 is null then
D___3
when D___3 is null then
D___4
when D___4 is null then
D___5
end as data
COALESCE(D___1, D___2, D___3, D___4, D___5) as DATA
您可以使用该函数。它返回给定列表中的第一个“NOTNULL”值
会是这样的:
select
case
when D___1 is null then
D___2
when D___2 is null then
D___3
when D___3 is null then
D___4
when D___4 is null then
D___5
end as data
COALESCE(D___1, D___2, D___3, D___4, D___5) as DATA
您可以使用该函数。它返回给定列表中的第一个“NOTNULL”值
会是这样的:
select
case
when D___1 is null then
D___2
when D___2 is null then
D___3
when D___3 is null then
D___4
when D___4 is null then
D___5
end as data
COALESCE(D___1, D___2, D___3, D___4, D___5) as DATA
您可以使用该函数。它返回给定列表中的第一个“NOTNULL”值
会是这样的:
select
case
when D___1 is null then
D___2
when D___2 is null then
D___3
when D___3 is null then
D___4
when D___4 is null then
D___5
end as data
COALESCE(D___1, D___2, D___3, D___4, D___5) as DATA
是否尝试显示D1、D2、D3、D4、D5中的第一个非空列?如果是这样,您最好使用
合并(D1、D2、D3、D4、D5)
你的案例陈述不会像你期望的那样有效,因为
a) “==”在Oracle中不是有效语法
b) 比较=null
(相应地,!=null
)总是返回null-ie。它既不是真的,也不是假的。相反,您应该检查:为null
(反之:不为null
)
c) 您的逻辑不完整-您正在检查D1到D5为空时该怎么办,而不是检查D1(或D2或…)不为空时该怎么办
假设您想要第一个非空值,这应该让您了解如何使用coalesce和case逻辑来实现这一点:
with sample_data as (select 1 col1, null col2, null col3 from dual union all
select null col1, 2 col2, null col3 from dual union all
select null col1, null col2, 3 col3 from dual union all
select null col1, 4 col2, 5 col3 from dual union all
select 6 col1, 7 col2, 8 col3 from dual union all
select 9 col1, null col2, 10 col3 from dual)
select col1,
col2,
col3,
coalesce(col1, col2, col3) first_non_null_coalesce,
case when col1 is not null then col1
when col2 is not null then col2
when col3 is not null then col3
end first_non_null_case_logic
from sample_data;
COL1 COL2 COL3 FIRST_NON_NULL_COALESCE FIRST_NON_NULL_CASE_LOGIC
---------- ---------- ---------- ----------------------- -------------------------
1 1 1
2 2 2
3 3 3
4 5 4 4
6 7 8 6 6
9 10 9 9
是否尝试显示D1、D2、D3、D4、D5中的第一个非空列?如果是这样,您最好使用
合并(D1、D2、D3、D4、D5)
你的案例陈述不会像你期望的那样有效,因为
a) “==”在Oracle中不是有效语法
b) 比较=null
(相应地,!=null
)总是返回null-ie。它既不是真的,也不是假的。相反,您应该检查:为null
(反之:不为null
)
c) 您的逻辑不完整-您正在检查D1到D5为空时该怎么办,而不是检查D1(或D2或…)不为空时该怎么办
假设您想要第一个非空值,这应该让您了解如何使用coalesce和case逻辑来实现这一点:
with sample_data as (select 1 col1, null col2, null col3 from dual union all
select null col1, 2 col2, null col3 from dual union all
select null col1, null col2, 3 col3 from dual union all
select null col1, 4 col2, 5 col3 from dual union all
select 6 col1, 7 col2, 8 col3 from dual union all
select 9 col1, null col2, 10 col3 from dual)
select col1,
col2,
col3,
coalesce(col1, col2, col3) first_non_null_coalesce,
case when col1 is not null then col1
when col2 is not null then col2
when col3 is not null then col3
end first_non_null_case_logic
from sample_data;
COL1 COL2 COL3 FIRST_NON_NULL_COALESCE FIRST_NON_NULL_CASE_LOGIC
---------- ---------- ---------- ----------------------- -------------------------
1 1 1
2 2 2
3 3 3
4 5 4 4
6 7 8 6 6
9 10 9 9
是否尝试显示D1、D2、D3、D4、D5中的第一个非空列?如果是这样,您最好使用
合并(D1、D2、D3、D4、D5)
你的案例陈述不会像你期望的那样有效,因为
a) “==”在Oracle中不是有效语法
b) 比较=null
(相应地,!=null
)总是返回null-ie。它既不是真的,也不是假的。相反,您应该检查:为null
(反之:不为null
)
c) 您的逻辑不完整-您正在检查D1到D5为空时该怎么办,而不是检查D1(或D2或…)不为空时该怎么办
假设您想要第一个非空值,这应该让您了解如何使用coalesce和case逻辑来实现这一点:
with sample_data as (select 1 col1, null col2, null col3 from dual union all
select null col1, 2 col2, null col3 from dual union all
select null col1, null col2, 3 col3 from dual union all
select null col1, 4 col2, 5 col3 from dual union all
select 6 col1, 7 col2, 8 col3 from dual union all
select 9 col1, null col2, 10 col3 from dual)
select col1,
col2,
col3,
coalesce(col1, col2, col3) first_non_null_coalesce,
case when col1 is not null then col1
when col2 is not null then col2
when col3 is not null then col3
end first_non_null_case_logic
from sample_data;
COL1 COL2 COL3 FIRST_NON_NULL_COALESCE FIRST_NON_NULL_CASE_LOGIC
---------- ---------- ---------- ----------------------- -------------------------
1 1 1
2 2 2
3 3 3
4 5 4 4
6 7 8 6 6
9 10 9 9
是否尝试显示D1、D2、D3、D4、D5中的第一个非空列?如果是这样,您最好使用
合并(D1、D2、D3、D4、D5)
你的案例陈述不会像你期望的那样有效,因为
a) “==”在Oracle中不是有效语法
b) 比较=null
(相应地,!=null
)总是返回null-ie。它既不是真的,也不是假的。相反,您应该检查:为null
(反之:不为null
)
c) 您的逻辑不完整-您正在检查D1到D5为空时该怎么办,而不是检查D1(或D2或…)不为空时该怎么办
假设您想要第一个非空值,这应该让您了解如何使用coalesce和case逻辑来实现这一点:
with sample_data as (select 1 col1, null col2, null col3 from dual union all
select null col1, 2 col2, null col3 from dual union all
select null col1, null col2, 3 col3 from dual union all
select null col1, 4 col2, 5 col3 from dual union all
select 6 col1, 7 col2, 8 col3 from dual union all
select 9 col1, null col2, 10 col3 from dual)
select col1,
col2,
col3,
coalesce(col1, col2, col3) first_non_null_coalesce,
case when col1 is not null then col1
when col2 is not null then col2
when col3 is not null then col3
end first_non_null_case_logic
from sample_data;
COL1 COL2 COL3 FIRST_NON_NULL_COALESCE FIRST_NON_NULL_CASE_LOGIC
---------- ---------- ---------- ----------------------- -------------------------
1 1 1
2 2 2
3 3 3
4 5 4 4
6 7 8 6 6
9 10 9 9
==不是SQL中的运算符,检查null应使用的是null。感谢提醒。==不是SQL中的运算符,检查null应使用的是null。感谢提醒。==不是SQL中的运算符,检查null应使用的是null。感谢提醒。==不是SQL中的运算符,检查null应该使用的是null。谢谢提醒。非常感谢!我是软件领域的新手,你的例子对我帮助很大!我们需要更多这样的答案。非常感谢!我是软件领域的新手,你的例子对我帮助很大!我们需要更多这样的答案。非常感谢!我是软件领域的新手,你的例子对我帮助很大!我们需要更多这样的答案。非常感谢!我是软件领域的新手,你的例子对我帮助很大!我们需要更多这样的答案。