2017-09-27 140 views
0

我怎样才能把这两个条件结合起来?SQL oracle如何结合两个条件

select ename, hiredate, next_day(last_day(hiredate)-7,5) 
from emp 
where to_number(to_char(hiredate, 'DD)) < 15 ORDER BY hiredate ASC; 

select ename, hiredate, last_day(next_day(last_day(hiredate), 5)) 
from emp 
where to_number(to_char(hiredate, 'DD)) > 15 ORDER BY hiredate ASC; 

我灵机一动,这两个存储变量,然后将它们连接起来。 但是,我目前刚刚接触sql oracle,但我不确定如何去做。 也许我可以做些什么?我希望你明白我想要完成的事情。

Define variable1 = next_day(last_day(hiredate)-7,5) 
where to_number(to_char(hiredate, 'DD)) < 15 
+0

添加一些示例表格数据和预期结果 - 作为格式化文本(即不是图片)。 – jarlh

回答

2

可以使用CASE表达式来确定渲染基于雇用日期哪个日期。

select 
    ename, 
    hiredate, 
    case when to_number(to_char(hiredate, 'DD')) < 15 
     then next_day(last_day(hiredate)-7,5) 
     when to_number(to_char(hiredate, 'DD')) > 15 
     then last_day(next_day(last_day(hiredate), 5)) end AS date 
from emp 
order by hiredate ASC; 

需要注意的是,目前尚不清楚为什么你不包括以下可能性:

to_number(to_char(hiredate, 'DD')) = 15 

也许你应该与其他两个条件,一斗吧。

+0

非常感谢您。现在很容易理解和使用大小写表达式。不过,我在第七行收到错误消息。由于某些原因,它不会将表名改为'date' – CaL17

+0

@ CaL17我使用'date'作为_alias_到'CASE'表达式。在你的结果集中,'CASE'列应该标记为'date';表名与它无关。 –