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 7491497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:03:55+00:00 2026-05-29T16:03:55+00:00

I need to update col1 in table1 with the highest value in val in

  • 0

I need to update col1 in table1 with the highest value in val in table2 that is lower than
col2 in table1. I am trying this:


    update table1 set col1 = max(t2.val)
    from table2 t2 where t2.VAL < col2

I am getting this error:
An aggregate may not appear in the set list of an UPDATE statement.

Here is sample data


Table1
Col1    Col2
2   null
3   null
4   null
6   null
7   null
8   null
9   null
10  null

Table2
Val
1
5

after I run the query I would want it to look like this:


Table1
Col1    Col2
2   1
3   1
4   1
6   5
7   5
8   5
9   5
10  5
  • 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-29T16:03:56+00:00Added an answer on May 29, 2026 at 4:03 pm
    UPDATE   t1     SET  
           modified = a.ddd from  t1 t  CROSS APPLY (SELECT MAX(t2.price) AS ddd  from t2 WHERE t2.price<t.val) a
    

    lets initialize t1 (the modified field is the one that will have the max value from t2 but less than its val value)

    enter image description here

    this is your table1 (t2) with values …

    enter image description here

    run the query : …

    UPDATE   t1     SET  
           modified = a.ddd from  t1 t  CROSS APPLY (SELECT MAX(t2.price) AS ddd  from t2 WHERE t2.price<t.val) a
    

    enter image description here

    edit

    for your values here is the result :

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this temp table that has structure like: col1 col2 col3 col3
SCENARIO: I have two tables, table1(col1 date) and table2(col2 varchar(20)). REQUIREMENT: Whenever anyone updated
I need to update a combobox with a new value so it changes the
I'm having problems with this query: SELECT col1, col2,col3,...,coln FROM MyTable WHERE col1 =
I need write an update statement that used multiple tables to determine which rows
I need to update textblock inside childWindow based on file name that is being
I need to update a single parameter within a large set of .xml files.
I need update a field value , increasing the old value. something like ths
I need to update same field to same value for hundreds of workitems in
This is how my table looks like.. id col1 col2 --------------- 1 a x

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.