2017-09-27 147 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';表名與它無關。 –