SQL将获取条件的数据作为oracle中两列的组合

时间:2016-01-13 12:43:34

标签: sql oracle

select * from table where 
COALESCE(HOURS, MINUTES) is not null 
and (if hours = null, then minutes >0)
and (if minutes = null then hours > 0)
and (minutes != 0 AND hours != 0)

如何将最后3个sql作为oracle的SQL条件?

示例数据:

Hours Minutes
----- -------
1     22
null  33
0     13
0     0
null  null
10    0
10    null

在这个场景中它应该返回

Hours Minutes
----- -------
1     22
null  33
0     13
10    0
10    null

4 个答案:

答案 0 :(得分:4)

我怀疑你想要:

where coalesce(hours, 0) + coalesce(minutes, 0) > 0

这基本上将NULL视为0,并确保总和大于零。

答案 1 :(得分:2)

编辑:更新了我的查询。感谢您的评论。

所以基本上你需要hoursminutes不是null0的行。我认为你需要这个(如果我理解你的要求)

with table1(Hours,Minutes) as
(select 1,22 from dual union
select null,33 from dual union
select 0,13 from dual union
select 0,0 from dual union
select null,null from dual union
select 10,0 from dual union
select 10,null from dual 
)
select * from table1
    where
        (hours is not null
        or 
        minutes is not null
        )
    and 
        (hours>0 or minutes>0)

答案 2 :(得分:1)

select * from table where 
COALESCE(HOURS, MINUTES) is not null 
and (minutes <> 0 AND hours <> 0)
and minutes>(case when hours is null then 0 else (minutes-1) end)
and hours>(case when minutes is null then 0 else (hours-1) end)

也许是这样的?因为它们可以为空而有点漏洞

答案 3 :(得分:1)

谢谢大家,根据您的建议,我能够获取正确的数据。我使用的条件如下:

从table中选择* where condition1         和(COALESCE(HOURS,MINUTES)不为空)         和(HOURS> 0 OR MINUTES&gt; 0)