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

  • SEARCH
  • Home
  • 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 3949394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:29:18+00:00 2026-05-20T01:29:18+00:00

I want to update the value of a column that is grouped by using

  • 0

I want to update the value of a column that is grouped by using as criteria two other columns. assume table A (a Char, b integer, c interger,d) and the a column can include same object name, so i want to update the table with the condition: if column’s a value and b value are found in two rows row1:(A1,2,numbery) and row2:(A1,2,numberx) then if: numberx,numbery>=0->AVG(third column), elsif numberx,numbery<0 THEN MIN else MAX (i.e -3,10->10).

--a--|--b--|--c--|--d--    desired TABLE update (A1,A2)   --a--|--b--|--c--|--d-- 
 A1     2     10    b                                     A1     2    "15"    b    
 A2     7     -9    bc                                    A2     7     "4"    bc         
 A3     7     12    fg                                    A3     7     12    fg
 A1     2     20    sa                                    A1     2    "15"    sa 
 A2     7      4    sa                                    A2     7    "4"     sa
  • 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-20T01:29:19+00:00Added an answer on May 20, 2026 at 1:29 am

    Create tables and sample data:

    SQL> create table t (a varchar2(20) not null
      2      , b number(38) not null
      3      , c number(38) not null
      4      , d varchar2(20) not null);
    
    Table created.
    
    SQL> insert into t values ('A1', 2, 10, 'b');
    
    1 row created.
    
    SQL> insert into t values ('A2', 7, -9, 'bc');
    
    1 row created.
    
    SQL> insert into t values ('A3', 7, 12, 'fg');
    
    1 row created.
    
    SQL> insert into t values ('A1', 2, 20, 'sa');
    
    1 row created.
    
    SQL> insert into t values ('A2', 7, 4, 'sa');
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    

    To help figure out update, create a query that gives the desired results, as I understand them: (Not part of the solution, just part of solving)

    SQL> select t.a, t.b
      2      , case when agg_t.min_c >= 0 then avg_c -- all values of c are >= 0
      3          when agg_t.max_c < 0 then min_c -- all values of c are < 0
      4          else agg_t.max_c end as c
      5      , t.d
      6  from (select a, b, min(c) as min_c
      7          , max(c) as max_c , avg(c) as avg_c
      8     from t
      9     group by a, b) agg_t
     10  inner join t on agg_t.a = t.a and agg_t.b = t.b
     11  /
    
    A                             B          C D
    -------------------- ---------- ---------- --------------------
    A3                            7         12 fg
    A1                            2         15 sa
    A2                            7          4 bc
    A2                            7          4 sa
    A1                            2         15 b
    

    Use merge to make available the agregate values during an update:

    SQL> merge into T dest
      2  using (select a, b, min(c) as min_c
      3          , max(c) as max_c , avg(c) as avg_c
      4     from t
      5     group by a, b) src
      6  on (dest.a = src.a and dest.b = src.b)
      7  when matched then update
      8      set c = case when src.min_c >= 0 then avg_c -- all values of c are >= 0
      9          when src.max_c < 0 then min_c -- all values of c are < 0
     10          else src.max_c end
     11  /
    
    5 rows merged.
    
    SQL> select * from t;
    
    A                             B          C D
    -------------------- ---------- ---------- --------------------
    A1                            2         15 b
    A2                            7          4 bc
    A3                            7         12 fg
    A1                            2         15 sa
    A2                            7          4 sa
    

    Rollback to initial test data:

    SQL> rollback;
    
    Rollback complete.
    

    This query will not update rows where a change is not actually being made, reducing locking, undo and redo:

    SQL> merge into T dest
      2  using (select a, b
      3          , case when min_c >= 0 then avg_c -- all values of c are >= 0
      4              when max_c < 0 then min_c -- all values of c are < 0
      5              else max_c end as new_c
      6      from (select a, b, min(c) as min_c
      7              , max(c) as max_c , avg(c) as avg_c
      8         from t
      9         group by a, b)) src
     10  on (dest.a = src.a and dest.b = src.b)
     11  when matched then update set c = new_c
     12  where c <> new_c
     13  or (c is null and new_c is not null)
     14  or (c is not null and new_c is null)
     15  /
    
    3 rows merged.
    
    SQL> 
    SQL> select * from t;
    
    A                             B          C D
    -------------------- ---------- ---------- --------------------
    A1                            2         15 b
    A2                            7          4 bc
    A3                            7         12 fg
    A1                            2         15 sa
    A2                            7          4 sa
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update my table so that every column that has a value
I want to update the lastlogin column value .i wrote the code like this
I want to check a Column value when update.If its match insert into another
I want to update a column by adding a new value alongside the old
i want to update multiple value of my table ORDRE ? table ORDRE CP
I have two tables nol_art and #tmpIzm I want to update nol_art with value
MySQL: In update trigger's body, can I obtain the value of a column that
I have created a database using SQLite. I want to update the value of
I have a table column that uses an enum type. I wish to update
POSTGRES- I want update the Employees.zipcode_mod column in the Employees table for the 'invalid

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.