SQL to generate a list of numbers from 1 to 100

后端 未结 11 1471
悲&欢浪女
悲&欢浪女 2020-11-27 04:07

Using the DUAL table, how can I get a list of numbers from 1 to 100?

相关标签:
11条回答
  • 2020-11-27 04:14

    A variant of Peter's example, that demonstrates a way this could be used to generate all numbers between 0 and 99.

    with digits as (
      select mod(rownum,10) as num 
      from   dual 
      connect by rownum <= 10
    )
    select a.num*10+b.num as num 
    from   digits a
           ,digits b
    order by num
    ;
    

    Something like this becomes useful when you are doing batch identifier assignment, and looking for the items that have not yet been assigned.

    For example, if you are selling bingo tickets, you may want to assign batches of 100 floor staff (guess how i used to fund raise for sports). As they sell a batch, they are given the next batch in sequence. However, people purchasing the tickets can select to purchase any tickets from the batch. The question may be asked, "what tickets have been sold".

    In this case, we only have a partial, random, list of tickets that were returned within the given batch, and require a complete list of all possibilities to determine which we don't have.

    with range as (
      select mod(rownum,100) as num 
      from   dual 
      connect by rownum <= 100
    ),
    AllPossible as (
      select a.num*100+b.num as TicketNum
      from   batches a
             ,range b
      order by num
    )
    select TicketNum as TicketsSold
    from   AllPossible
    where  AllPossible.Ticket not in (select TicketNum from TicketsReturned)
    ;
    

    Excuse the use of key words, I changed some variable names from a real world example.

    ... To demonstrate why something like this would be useful

    0 讨论(0)
  • 2020-11-27 04:21

    Peter's answer is my favourite, too.

    If you are looking for more details there is a quite good overview, IMO, here.
    Especially interesting is to read the benchmarks.

    0 讨论(0)
  • 2020-11-27 04:22

    I created an Oracle function that returns a table of numbers

    CREATE OR REPLACE FUNCTION [schema].FN_TABLE_NUMBERS(
        NUMINI INTEGER,
        NUMFIN INTEGER,
        EXPONENCIAL INTEGER DEFAULT 0
    ) RETURN TBL_NUMBERS
    IS
        NUMEROS TBL_NUMBERS;
        INDICE NUMBER;
    BEGIN
        NUMEROS := TBL_NUMBERS();
    
        FOR I IN (
            WITH TABLA AS (SELECT NUMINI, NUMFIN FROM DUAL)
            SELECT NUMINI NUM FROM TABLA UNION ALL
            SELECT 
                (SELECT NUMINI FROM TABLA) + (LEVEL*TO_NUMBER('1E'||TO_CHAR(EXPONENCIAL))) NUM
            FROM DUAL
            CONNECT BY 
                (LEVEL*TO_NUMBER('1E'||TO_CHAR(EXPONENCIAL))) <= (SELECT NUMFIN-NUMINI FROM TABLA)
        ) LOOP
            NUMEROS.EXTEND;
            INDICE := NUMEROS.COUNT; 
            NUMEROS(INDICE):= i.NUM;
        END LOOP;
    
        RETURN NUMEROS;
    
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
           RETURN NUMEROS;
      WHEN OTHERS THEN
           RETURN NUMEROS;
    END;
    /
    

    Is necessary create a new data type:

    CREATE OR REPLACE TYPE [schema]."TBL_NUMBERS" IS TABLE OF NUMBER;
    /
    

    Usage:

    SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10))--integers difference: 1;2;.......;10
    

    And if you need decimals between numbers by exponencial notation:

    SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10,-1));--with 0.1 difference: 1;1.1;1.2;.......;10
    SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10,-2));--with 0.01 difference: 1;1.01;1.02;.......;10
    
    0 讨论(0)
  • 2020-11-27 04:24
    SELECT * FROM `DUAL` WHERE id>0 AND id<101
    

    The above query is written in SQL in the database.

    0 讨论(0)
  • 2020-11-27 04:27

    Your question is difficult to understand, but if you want to select the numbers from 1 to 100, then this should do the trick:

    Select Rownum r
    From dual
    Connect By Rownum <= 100
    
    0 讨论(0)
  • 2020-11-27 04:28

    Using Oracle's sub query factory clause: "WITH", you can select numbers from 1 to 100:

    WITH t(n) AS (
      SELECT 1 from dual
      UNION ALL
        SELECT n+1 FROM t WHERE n < 100
    )
    SELECT * FROM t;
    
    0 讨论(0)
提交回复
热议问题