Lets say we have a table here, populated with the following data:
acc_id1 acc_id2 acc_grp_id1 acc_parameter1 acc_parameter2
111 111111 115 1 155
111 111111 116 2 166
222 222222 216 2 167
222 222222 217 2 168
333 333333 316 3 155
334 333333 317 3 169
444 444444 416 4 151
445 444444 417 2 166
Every account has multiple acc_ids, but the parameter codes are fixed. I am trying to retrieve only those accounts which follow this rule: if the biggest (latest) acc_grp_id1 has param1 = 2 and param2 = 166, both rows should be present. If the biggest (latest) parameters are equal to different codes, populate only the latest one.
This is how it should look:
acc_id1 acc_id2 acc_grp_id1 acc_parameter1 acc_parameter2
111 111111 115 1 155
111 111111 116 2 166
222 222222 217 2 168
334 333333 317 3 169
444 444444 416 4 151
445 444444 417 2 166
The actual output is a join from 5 tables.
Any ideas would be appreciated.
Try this(not tested):