Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:02:24+00:00 2026-05-24T12:02:24+00:00

I have an update query that should update a field with the most frequent

  • 0

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 :

  1. I can’t put an ORDER BY inside the select of an update
  2. 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_A gives 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:02:25+00:00Added an answer on May 24, 2026 at 12:02 pm

    This can be done in Oracle uses analytic functions; don’t know whether the same code would work in PostgreSQL.

    Creating a target table:

    SQL> create table t23
      2    as select deptno, cast (null as number(7,2)) max_sal from dept
      3  /
    
    Table created.
    SQL> 
    

    Now, the update:

    SQL> update t23
      2  set max_sal = ( select sal from
      3                   ( select deptno, sal
      4                     , row_number() over (partition by deptno order by sal desc) rn
      5                      from emp )
      6                 where rn = 1
      7                 and deptno = t23.deptno )
      8  /
    
    6 rows updated.
    
    SQL>
    

    Here is the outcome:

    SQL> select * from t23
      2  /
    
        DEPTNO    MAX_SAL
    ---------- ----------
            10       5000
            20       3000
            30       3750
            40
            50       4500
            60
    
    6 rows selected.
    
    SQL> 
    

    Just to confirm the results…

    SQL> select deptno, max(sal)
      2  from emp
      3  where sal is not null
      4  group by deptno
      5  order by deptno
      6  /
    
        DEPTNO   MAX(SAL)
    ---------- ----------
            10       5000
            20       3000
            30       3750
            50       4500
    
    SQL> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this SQL update query: UPDATE table1 SET table1.field1 = 1 WHERE table1.id
I have an update query being run by a cron task that's timing out.
I have a query that is ExcelQuery = Update [Sheet1$] +set CITIZEN_ID = #
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have set up an update query which will update values entered into text
I am trying to update my table 1 with some field value from table
I have a statement to update a field if a related table record exists.
I have a query that updates one record, and only one record. Is there
I have this update statement: mysql_query (UPDATE loan SET loan_reff_id='$_POST[loan_reff_id]', commit_date='$_POST[commit_date]',app_loan_type='Tertiary Loan', app_ln_amnt='$_POST[app_ln_amnt]', institution_name='$_POST[institution_name]',
I have some funny deadlock caused by a stupid simple SQL UPDATE query, on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.