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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:33:41+00:00 2026-06-18T02:33:41+00:00

I have two tables like the following: TABLE1: ======= somid, tobeupdated 1 , null

  • 0

I have two tables like the following:

TABLE1:
=======
somid, tobeupdated
1    ,  null
2    ,  null
3    ,  null
10   ,  null

TABLE2:
=======
rangeofids
2
3
9
10
11
12
13

I have to update TABLE1.tobeupdated (or found its’ should be value) based on the following criteria(s):

  1. if TABLE1.somid NOT exists in TABLE2.rangeofids, then the expected result is: tobeupdated = TABLE1.somid
  2. else find the next available (or unused) TABLE2.rangeofids which is larger then TABLE1.somid

So the expected values are:bu

TABLE1:
=======
somid, tobeupdated
1    ,  1
2    ,  4
3    ,  4
10   ,  14

I tried hard, but the simplest solution I came up with is creating a temporary table with a full sequence of ids (from 1 to max(rangeofids)+1) MINUS TABLE2.rangeofids so I can found the MIN(TMPTABLE.id) where TMPTABLE.ID > TABLE1.somid.

But isn’t there a better solution (without the temp table)?

Note: I can’t create procedures/functions, etc, so it must be standard (Oracle 10) SQL.

  • 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-06-18T02:33:42+00:00Added an answer on June 18, 2026 at 2:33 am

    This is my try.

    First we should decide using only table2 what value should return after finding the value there.

    select rangeofids, 
      candidate, 
      nvl(candidate,lead(candidate ignore nulls) over (order by rangeofids)) as full_candidate
    from (
        select rangeofids, case when dist=1 then null else rangeofids+1 end as candidate
        from (
            select rangeofids,
                   lead(rangeofids) over (order by rangeofids) - rangeofids as dist
            from table2
            )
          );
    

    After this a merge into table1 with the below select will solve the problem:

    select someid, nvl(full_candidate, someid) 
    from table1 a
    left join (    
        --the above query
    ) b
    on a.someid = b.rangeofids;
    

    See SQLFIDDLE.

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

Sidebar

Related Questions

I have a scenario like this: There are two tables table1 and table2 .
Suppose I have two tables Main and Other like the following: Main +----------+-----------------------------------+ |
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
Let's say that I have two tables in a database: Table1 and Table2 .
I have two tables that look like following: RELEASE TABLE name asin MATCHES TABLE
I use SQL Server 2000. Suppose I have two tables like the following: Area
I have the following two tables with data like so: Table Values var_label 1
I have two tables. Table1 and Table2 Table1 id tid 1 100 2 200
I have two tables that I would like to join with the following columns:

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.