简体   繁体   中英

SQL to get data with condition as combination of two columns in 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)

How can I give last 3 sqls as SQL condition for oracle?

Sample data:

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

In this scenerio it should return

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

I suspect that you want:

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

This basically treats NULL as 0 and makes sure that the sum is greater than zero.

Edit: Updated my query. Thanks for the comment.

So basically you need rows where either hours or minutes is not null or 0 . I think you need this (If I understand your requirement correctly)

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)
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)

maybe something like this? a little porblematic since they can be null

Thanks guys, based on your suggestions, i am able to fetch proper data. I am using conditions like:

select * from table where condition1 and (COALESCE(HOURS, MINUTES) is not null) and (HOURS > 0 OR MINUTES > 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM