开发者

Use Case is postgresql query to get record based on condition

I have two table a) tblresign and b) tblemp

tblresign has one foreign key which ref开发者_如何学Pythoner the tblemp primary key

tblresign have on column leavingdate tblemp have on column retiredate

i want to write a query which will check condition that if leavingdate is present than it will not give retire date and if retiredate is present it will not give leaveingdate that mean any how i need only one date from both of them which is present

can any one help to how to write that select query in postgresql


Not sure I understand your question, but what about this:

SELECT e.emp_name,
       e.id,
       coalesce(r.leavingdate, e.retiredate) as some_date 
FROM tblemp e
   LEFT JOIN tblresign r ON e.id = r.emp_id

If there is no row in tblresign for an employee or that row's leavingdate is null then retiredate is shown.

You did not state which date to show if both dates are present. You might need to change the order in the coalesce() function to suit your needs. If you have a more complex rule, then you probably want a CASE statement to cover that. But we need way more information in order to answer that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜