Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Serial number in SQL

Serial number in SQL

Scheduled Pinned Locked Moved Database
databasehelptutorial
4 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    draghu
    wrote on last edited by
    #1

    Hello, I have a table ABC with two columns NAME and SEQUENCE. The valid values of sequence are from 1..50. The table may not have all the Sequence number. For example following is the data in my table.. NAME SEQUENCE ------------------------ Jonh| 5 Amy| 1 Suresh| 3 I need a SQL to get the data like NAME SEQUENCE ------------------------ Amy| 1 | 2 Suresh| 3 | 4 Jonh | 5 |6 |7 .. .. .. |50 Basically I want to have rows for the sequence numbers which are not present i my table. I thought of using the following query to generate a sequence and then join, but does not help

    select rownum from dual connect by rownum <=50

    I tried something like

    with temp as (select name, sequence from abc where supp_ref_order is not null order by sequence )
    select rownum as num, temp.name from dual, temp connect by rownum <=50 where temp.sequence =num

    P A 2 Replies Last reply
    0
    • D draghu

      Hello, I have a table ABC with two columns NAME and SEQUENCE. The valid values of sequence are from 1..50. The table may not have all the Sequence number. For example following is the data in my table.. NAME SEQUENCE ------------------------ Jonh| 5 Amy| 1 Suresh| 3 I need a SQL to get the data like NAME SEQUENCE ------------------------ Amy| 1 | 2 Suresh| 3 | 4 Jonh | 5 |6 |7 .. .. .. |50 Basically I want to have rows for the sequence numbers which are not present i my table. I thought of using the following query to generate a sequence and then join, but does not help

      select rownum from dual connect by rownum <=50

      I tried something like

      with temp as (select name, sequence from abc where supp_ref_order is not null order by sequence )
      select rownum as num, temp.name from dual, temp connect by rownum <=50 where temp.sequence =num

      P Offline
      P Offline
      Pablo Aliskevicius
      wrote on last edited by
      #2

      I guess you're using Oracle, since you select from 'dual'. You may: 1. Declare a temporary table:

      CREATE GLOBAL TEMPORARY TABLE temp_number
      ( number_column NUMBER( 10, 0 )
      )
      ON COMMIT DELETE ROWS;

      2. Fill it with numbers from 1 to 50.

      INSERT INTO temp_number
      SELECT rownum FROM ALL_OBJECTS
      WHERE rownum BETWEEN 1 AND 50

      You may prefer a for loop. 3. Outer-join it to your table.

      SELECT number_column, NAME
      FROM TEMP_NUMBER
      LEFT OUTER JOIN ABC
      ON TEMP_NUMBER.number_column = ABC.SEQUENCE
      ORDER BY number_column

      Hope this helps, Pablo.

      Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).

      1 Reply Last reply
      0
      • D draghu

        Hello, I have a table ABC with two columns NAME and SEQUENCE. The valid values of sequence are from 1..50. The table may not have all the Sequence number. For example following is the data in my table.. NAME SEQUENCE ------------------------ Jonh| 5 Amy| 1 Suresh| 3 I need a SQL to get the data like NAME SEQUENCE ------------------------ Amy| 1 | 2 Suresh| 3 | 4 Jonh | 5 |6 |7 .. .. .. |50 Basically I want to have rows for the sequence numbers which are not present i my table. I thought of using the following query to generate a sequence and then join, but does not help

        select rownum from dual connect by rownum <=50

        I tried something like

        with temp as (select name, sequence from abc where supp_ref_order is not null order by sequence )
        select rownum as num, temp.name from dual, temp connect by rownum <=50 where temp.sequence =num

        A Offline
        A Offline
        Andy_L_J
        wrote on last edited by
        #3

        This is not Oracle but may give you some useful pointers...Using User Defined Functions in Table Inserts and Seeding with Stored Procedures[^] Might be worth a look.

        I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

        D 1 Reply Last reply
        0
        • A Andy_L_J

          This is not Oracle but may give you some useful pointers...Using User Defined Functions in Table Inserts and Seeding with Stored Procedures[^] Might be worth a look.

          I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

          D Offline
          D Offline
          draghu
          wrote on last edited by
          #4

          I got another solution

          select * from ABC t,( select level R from dual connect by level <= 6) t1
          where t.SEQUENCE(+) = t1.r
          order by r

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups