0

I have a table approval like below..

m_requisition_id ad_wf_activity_id bpname designation
1014546 1014546 Abul Kalam Azad Asst. Manager
1014546 1225097 Md. Hasan Zahir Plant Manager
1014546 1229239 Md. Hasan Zahir Plant Manager
1014546 1229240 Sayela Alam Sr. Manager
1014546 1229241 Md. Hasan Zahir Plant Manager
1014546 1229242 Sayela Alam Sr. Manager

Now i need bpname column's distinct value based on ad_wf_activity_id column's maximum number like below.

m_requisition_id ad_wf_activity_id bpname designation
1014546 1014546 Abul Kalam Azad Asst. Manager
1014546 1229241 Md. Hasan Zahir Plant Manager
1014546 1229242 Sayela Alam Sr. Manager

2 Answers 2

1

You can use max() function instead of distinct on like below

select m_requisition_id, max(ad_wf_activity_id), bpname, designation
from approval
GROUP BY m_requisition_id, bpname, designation

Lets try here- https://dbfiddle.uk/?rdbms=postgres_12&fiddle=73f15e1ed96f1308a5984a91b1df91e2

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

Comments

1

That would be DISTINCT ON:

SELECT DISTINCT ON (m_requisition_id, bpname, designation)
       m_requisition_id, bpname, designation
FROM atable
ORDER BY m_requisition_id, bpname, designation, ad_wf_activity_id DESC;

Your data model is not normalized, you you are prone to problems caused by slightly different spellings.

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.