ORACLE SQL:Get all integers between two numbers

前端 未结 13 1134
说谎
说谎 2020-12-03 13:42

Is there any way to select the numbers (integers) that are included between two numbers with SQL in Oracle; I don\'t want to create PL/SQL procedure or function.

Fo

相关标签:
13条回答
  • 2020-12-03 13:58

    this single line query will help you,

    select level lvl from dual where level<:upperbound and 
    
                                  level >:lowerbound connect by level<:limt
    

    For your case:

    select level lvl from dual where level<10 and level >3 connect by level<11
    

    let me know if any clarification.

    0 讨论(0)
  • 2020-12-03 13:59

    This is a late addition. But the solution seems to be more elegant and easier to use.

    It uses a pipelined function that has to be installed once:

    CREATE TYPE number_row_type AS OBJECT 
    (
      num NUMBER
    );
    
    CREATE TYPE number_set_type AS TABLE OF number_row_type;
    
    CREATE OR REPLACE FUNCTION number_range(p_start IN PLS_INTEGER, p_end IN PLS_INTEGER)
        RETURN number_set_type
        PIPELINED
    IS
        out_rec number_row_type := number_row_type(NULL);
    
    BEGIN
      FOR i IN p_start .. p_end LOOP
        out_rec.num := i;
        pipe row(out_rec);
      END LOOP;
    
    END number_range;
    /
    

    Then you can use it like this:

    select * from table(number_range(1, 10));
    
    NUM
    ---
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
    

    The solution is Oracle specific.

    0 讨论(0)
  • 2020-12-03 14:05

    The first thing I do when I create a new database is to create and populate some basic tables.

    One is a list of all integers between -N and N, another is a list of dates 5 years in the past through 10 years in the future (a scheduled job can continue creating these as needed, going forward) and the last is a list of all hours throughout the day. For example, the inetgers:

    create table numbers (n integer primary key);
    insert into numbers values (0);
    insert into numbers select n+1 from numbers; commit;
    insert into numbers select n+2 from numbers; commit;
    insert into numbers select n+4 from numbers; commit;
    insert into numbers select n+8 from numbers; commit;
    insert into numbers select n+16 from numbers; commit;
    insert into numbers select n+32 from numbers; commit;
    insert into numbers select n+64 from numbers; commit;
    insert into numbers select n+128 from numbers; commit;
    insert into numbers select n+256 from numbers; commit;
    insert into numbers select n+512 from numbers; commit;
    insert into numbers select n+1024 from numbers; commit;
    insert into numbers select n+2048 from numbers; commit;
    insert into numbers select n+4096 from numbers; commit;
    insert into numbers select n+8192 from numbers; commit;
    insert into numbers select -n from numbers where n > 0; commit;
    

    This is for DB2/z which has automatic transaction start which is why it seems to have naked commits.

    Yes, it takes up a (minimal) space but it makes queries much easier to write, simply by selecting values from those tables. It's also very portable across pretty much any SQL-based DBMS.

    Your particular query would then be a simple:

    select n from numbers where n >=3 and n <= 10;
    

    The hour figures and date ranges are quite useful for the sort of reporting applications we work on. It allows us to create zero entries for those hours of the day (or dates) which don't have any real data so that, instead of (where there's no data on the second of the month):

    Date       | Quantity
    -----------+---------
    2009-01-01 |        7
    2009-01-03 |       27
    2009-01-04 |        6
    

    we can instead get:

    Date       | Quantity
    -----------+---------
    2009-01-01 |        7
    2009-01-02 |        0
    2009-01-03 |       27
    2009-01-04 |        6
    
    0 讨论(0)
  • 2020-12-03 14:05
    SQL> var N_BEGIN number
    SQL> var N_END number
    SQL> exec :N_BEGIN := 3; :N_END := 10
    
    PL/SQL procedure successfully completed.
    
    SQL>  select :N_BEGIN + level - 1 n
      2     from dual
      3  connect by level <= :N_END - :N_BEGIN + 1
      4  /
    
             N
    ----------
             3
             4
             5
             6
             7
             8
             9
            10
    
    8 rows selected.
    

    This uses the same trick as Tony's. Note that when you are using SQL*Plus 9, you have to make this query an inline view as Tony showed you. In SQL*Plus 10 or higher, the above is sufficient.

    Regards, Rob.

    0 讨论(0)
  • 2020-12-03 14:06

    Or you can use Between

    Select Column1 from dummy_table where Column2 Between 3 and 10
    
    0 讨论(0)
  • 2020-12-03 14:09

    You can use the MODEL clause for this.

    SELECT c1 from dual
      MODEL DIMENSION BY (1 as rn)  MEASURES (1 as c1)
      RULES ITERATE (7)
      (c1[ITERATION_NUMBER]=ITERATION_NUMBER+7)
    
    0 讨论(0)
提交回复
热议问题