0

I would like to write one select statement in postgres how could I place if condition in where condition

To creating a report in that suppose we have 20 country in our table and and we have three type of employee which having contact, payroll and permanent these are the employee_category I would like to get all employee details except India and USA who having contact employee in these two country if employee having payroll and permanent should come in result

something like this

SELECT
r.employee_id AS E_ID,
r.Joining_date AS J_DATE,
r.employee_type AS E_TYPE,
'NIL'::text AS REMARKS,
i.source as SOURCE,
i.user_orgname as USER_ORGNAME
from master_employee r
INNER JOIN users_master AS i ON r.user_id = i.loginid and  i.userstatus = 'Active' 
WHERE 
if(country="india","USA"){
employee_category!='contract'
}
 and
date(r.createddate) = date(now())
ORDER BY r.createddate ASC ;
4
  • Are you using MySQL or Postgresql? Commented Nov 12, 2020 at 7:18
  • I'm using PostgreSQL Commented Nov 12, 2020 at 18:51
  • Gordon's answer is right but you may prefer: where not (country in ('india', 'USA') and employee_category = 'contract') and ... Commented Nov 12, 2020 at 20:09
  • I'm creating a report in that suppose we have 20 country in our table and and we have two type of employee which having contact, payroll and permanent these are the employee_category i would like to get all employee details except India and USA who having contact employee in these two country if employee having payroll and permanent should come in result @George query is working but the 215 record is missing Commented Nov 12, 2020 at 22:33

2 Answers 2

2

Using the case statement should work.

SELECT
r.employee_id AS E_ID,
r.Joining_date AS J_DATE,
r.employee_type AS E_TYPE,
'NIL'::text AS REMARKS,
i.source as SOURCE,
i.user_orgname as USER_ORGNAME
from master_employee r
INNER JOIN users_master AS i ON r.user_id = i.loginid and  i.userstatus = 'Active' 
WHERE employee_category <> case when country in ("india","USA") then 'contract' end
 and date(r.createddate) = date(now())
ORDER BY r.createddate ASC ;
Sign up to request clarification or add additional context in comments.

2 Comments

The implied else in your case expression will evaluate to null and then the comparison will fail. Not sure what OP wants here exactly. Of course the error is the result of using double quotes.
I'm creating a report in that suppose we have 20 country in our table and and we have three type of employee which having contact, payroll and permanent these are the employee_category i would like to get all employee details except India and USA who having contact employee in these two country if employee having payroll and permanent should come in result @George query is working but the 215 record is missing
1

Instead, use basic boolean logic:

where (country not in ('india', 'USA') or
       employee_category <> 'contract'
      ) and
      date(r.createddate) = date(now())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.