3

I have database table name emp_leave in postgreSQL9.4 like

emp_name leave_type total_lday
Tame PL 3
Tame UL 4

I want the query result like

emp_name paid_leave unpaid_leave
Tame 3 4

2 Answers 2

7

Postgresql 9.4 also has the FILTER clause to do conditional aggregation:

select emp_name, 
       max(total_lday) FILTER (where leave_type = 'PL') as paid_leave,
       max(total_lday) FILTER (where leave_type = 'UL') as unpaid_leave
from emp_leave
group by emp_name

(ANSI SQL-2016, Feature T612, “Advanced OLAP operations”.)

Sign up to request clarification or add additional context in comments.

Comments

3

You can try below - using conditional aggregation

select emp_name, 
  max(case when leave_type='PL' then total_lday end) as paid_leave,
  max(case when leave_type='UL' then total_lday end) as unpaid_leave
from emp_leave
group by emp_name

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.