1

I recently encounter a problem in sql ,

I have a table called Plan which looks like as below order by Distance ...

 Distance Destination
 0         a
 1        null
 2        null
 3         b
 .        null
 .        null
 1050      c

I have two value as input from the user like a and b or b and c , how can I just get this range of value by using SQL

what I did is select the distance of a and b , and re-select all the list by doing distance > a.distance and distance < b.distance . any idea of how to write this more efficient and more clever in one sql ? sorry for this noob question ; my desire result is like when you input :b and c

return a list

 0         b
 .        null
 .        null
 1047      c
WickedFAN
  • 65
  • 6

1 Answers1

1

Use with SQL statement

    WITH agreater AS (
        SELECT 
            * 
        FROM 
            Plan 
        WHERE 
            distance >= (select distance from Plan where destination = 'a')
    )
    SELECT 
        * 
    FROM 
        agreater
    WHERE 
        distance <= (select distance from agreater where destination = 'c')

You can take a look at this answer to get some more info about with

Community
  • 1
  • 1
m.antkowicz
  • 13,268
  • 18
  • 37