开发者

Use Case is postgresql query to get record based on condition

开发者 https://www.devze.com 2023-04-03 06:40 出处:网络
I have two table a) tblresign and b) tblemp tblresign has one foreign key which ref开发者_如何学Pythoner the tblemp primary key

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

精彩评论

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