Skip to content
Advertisement

Display group having certain value only on max date and not in other entries of group

I am looking to display the group which is grouped on id column where trans_cd='Audit' only on max trans_proc_dt of that group. There should not be any other trans_cd='Audit' execpt for max trans_proc_dt.

My output should be

I can’t think how to go about getting my result set.

Advertisement

Answer

You can use analytic functions to calculate absolute maximum date and minimum date of Audit code per group, then compare them: if no more such transactions occurred on other dates, they should be equal.

 ID | TRANS_PROC_DT | TRANS_CD     | TRANS_AMT
--: | :------------ | :----------- | --------:
511 | 4/27/2020     | Renewal      |      4409
511 | 7/30/2020     | Change       |         0
511 | 10/5/2020     | Cancellation |     -2558
511 | 2/18/2021     | Audit        |      2806
984 | 5/13/2020     | Renewal      |      1081
984 | 11/2/2020     | Change       |         0
984 | 6/3/2021      | Audit        |         0

db<>fiddle here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement