I've tried to achieve this but I'm still having multiple identical items in column item_name even using DISTINCT.
this is what I got
contract_id | item_name | max(amount) |
1 | item1 | 20
12 | item1 | 25
111 | item1 | 10
3 | item2 | 15
32 | item2 | 30
but this is what I want
contract_id | item_name | max(amount) |
12 | item1 | 25
32 | item2 | 30
SELECT DISTINCT tb2.item_detail_id
tb2.item_name
tb1.contract_id
max(tb1.amount)
FROM main_table tb1
INNER JOIN (SELECT DISTINCT t_tb2.item_detail_id
MAX(t_tb1.amount)
FROM main_table t_tb1
INNER JOIN item t_tb2
ON t_tb1.item_detail_id = t_tb2.item_detail_id
GROUP BY t_tb2.item_detail_id) temp
ON tb1.item_detail_id = temp.item_detail_id
INNER JOIN item tb2
ON tb1.item_detail_id = tb2.item_detail_id
GROUP BY tb2.item_name,
tb1.contract_id,
tb1.amount