Example (a):
Code:
INSERT INTO temp
SELECT product_id , invoice_nbr , creation_dt , charge_dt
, invoice_amt , ifee , invoice_status
FROM (SELECT a.*
, ROW_NUMBER()
OVER(PARTITION BY product_id
ORDER BY charge_dt DESC) AS r_n
FROM table_a a
) a
WHERE r_n = 1
;
See this thread too.
Max and group by gives error
In Example (a),
PARTITION BY was addded to select within each product_id
and ROW_NUMBER was used instead of RANK to gurantee only one row to be selected for each product_id.
Use ROW_NUMBER or RANK depending on your requirements.