I have an update query that should update a field with the most frequent values from another table
this is how i do it in postgreSQL
UPDATE TABLE1 T1
SET COLUMN_B = (SELECT COLUMN_B
FROM
(SELECT COLUMN_A,COLUMN_B, COUNT(1) AS FREQUENCY
FROM TABLE2 T2
GROUP BY COLUMN_A,COLUMN_B
ORDER BY COLUMN_A,FREQUENCY DESC) QUERY1
WHERE QUERY1.COLUMN_A= T1.COLUMN_A
GROUP BY COLUMN_A,COLUMN_B,FREQUENCY
ORDER BY FREQUENCY DESC LIMIT 1
)
this works fine in postgresql , i want to do the same query in Oracle using rownum =1 but I have several issues :
- I can’t put an ORDER BY inside the select of an update
- If i decide to put the order by in the nested select (QUERY1), the nested select doesn’t understand the reference to table T1 (
T2.COLUMN_A = T1.COLUMN_Agives an error that T1.COLUMN_A invalid identifier)
How can i do this in oracle? what i want is that T1.COLUMN_B be filled with the most frequent common value of COLUMN_B FROM T2 for each COLUMN_A.
Thank you for any help or suggestion
This can be done in Oracle uses analytic functions; don’t know whether the same code would work in PostgreSQL.
Creating a target table:
Now, the update:
Here is the outcome:
Just to confirm the results…