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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:17:04+00:00 2026-06-04T13:17:04+00:00

given the following Table1: RefID intVal SomeVal ———————- 1 10 val01 1 20 val02

  • 0

given the following Table1:

    RefID  intVal  SomeVal
    ----------------------
        1      10    val01
        1      20    val02
        1      30    val03
        1      40    val04
        1      50    val05
        2      10    val06
        2      20    val07
        2      30    val08
        2      40    val09
        2      50    val10
        3      12    val11
        3      14    val12
        4      10    val13
        5     100    val14
        5     150    val15
        5    1000    val16

and Table2 containing some RefIDs and intVals like

    RefID  intVal
    -------------
        1      11    
        1      28    
        2       9    
        2      50    
        2      51    
        4      11    
        5       1    
        5     150    
        5     151    

need an SQL Statement to get the next greater intValue for each RefID and NULL if not found in Table1
following is the expected result

    RefID  intVal  nextGt  SomeVal 
    ------------------------------
        1      11      20  val01
        1      28      30  val03
        2       9      10  val06
        2      50      50  val10
        2      51    NULL   NULL
        4      11    NULL   NULL
        5       1     100  val14
        5     150     150  val15
        5     151    1000  val16

help would be appreciated !

  • 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-04T13:17:06+00:00Added an answer on June 4, 2026 at 1:17 pm

    Derived table a retrieves minimal values from table1 given refid and intVal from table2; outer query retrieves someValue only.

    select a.refid, a.intVal, a.nextGt, table1.SomeVal
    from
    (
        select table2.refid, table2.intval, min (table1.intVal) nextGt
          from table2
          left join table1
            on table2.refid = table1.refid
           and table2.intVal <= table1.intVal
         group by table2.refid, table2.intval
    ) a
    -- table1 is joined again to retrieve SomeVal 
    left join table1
      on a.refid = table1.refid
     and a.nextGt = table1.intVal
    

    Here is Sql Fiddle with live test.

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

Sidebar

Related Questions

Given the following tables: Table1 [class] [child] math boy1 math boy2 math boy3 art
given the following table1 id,name 1,test1 2,test2 3,test3 tablePrt id,date 1,5/1/11 1,5/3/11 3,3/1/11 tableOnl
Given: A table named Table1 that has the following columns: ID ColumnA ColumnB Typing
Given the following tables: Topic id, last_updated_child_id Response id, topic_id, updated_at How do I
Given the following tables: shows: title | basic_ticket_price ----------------+-------------------- Inception | $3.50 Romeo &
Given the following table PAYMENT_Date TRANSACTION_TYPE PAYMENT_AMT 1/1/2012 P 184366 1/1/2012 R -5841 1/2/2012
Given the following Tables: Names Id int Name varchar Properties Id int NameId varchar
Given the following html table and script shown below I am having a problem
Problem Given the following two tables, I'd like to select all Ids for Posts
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE

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.