Need SQL statement to generate a sequence number in the output rows

Hi folks. I need to create an SQL statement that generates a sequence number column in the output rows (records) such that the first returned row has this column set to 1, second returned row has the column set to 2, etc.
For example, consider the query:
SELECT income from employees WHERE income != 20000 ORDER BY income;
If employees.income contains 60,000, 20,000, 35,000, and 19,000 for respective rows, the output would be this:
19,000
35,000
60,000
I would like the SQL to also return a sequence number that is computed across the returned rows, resulting in two output columns:
1 19,000
2 35,000
3 60,000
Is there a simple SQL function that generates the sequence number, in order, and only for the returned rows? Or is there another way?
I'm stumped. Any help is appreciated! Thanks!
- Jack Cochrane

Hi,
Welcome to the forum!
Use ROWNUM, like (example):
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
SQL> select rownum, first_name from (select e.first_name from employees e where e.first_name like 'J%' order by e.first_name);
    ROWNUM FIRST_NAME
         1 Jack
         2 James
         3 James
         4 Janette
         5 Jason
         6 Jean
         7 Jennifer
         8 Jennifer
         9 John
        10 John
        11 John
        12 Jonathon
        13 Jose Manuel
        14 Joshua
        15 Julia
        16 Julia
16 rows selected
SQL> But rememeber if you want to be sure of unique numbers in certain field is better to use sequences and use seq_name.nextval each time you need.
Regards,

Similar Messages

  • How to change the sequence number in the test packages?

    Hello,
    I need to change the sequence number in the test packages. Currently I use "move test case." However you can only move one item at a time.
    There is another option to make this change?
    Best regards,
    Elisabete

    Hi Elisabete,
    Sorry, I've to acknowledge the reply from Fabricius.
    That's poor in release 7.0. A former version provided a solution where it was possible to move a testcase even over pages at once.  May be SAP changed this functionality because of other problems caused by the former solution.
    Therefore I am looking for a business blueprint structure meeting the logical and operational process for little need of changes in sequences.  And I create E2E-scenarios / E2E-processes as an additional structure in a project to have an easier approach in the Test Workbench.  An other option is to divide test packages into smaller ones.
    I hope we can look foreward to a better and more comfortable solution in release 7.1.
    Best regards,
    Adelbert

  • Scanner(Warning) : 027: Unable to read the sequence number from the Workstation object.

    Hi
    We are having trouble storing inventory scans from some workstations.
    We have a windows ONLY environment, with middle tier servers. (ZEN65SP1,
    W2KSP4).
    Some workstations are storing fine. The Storer function is working and we
    can see the storer functions for the 'good' workstations in the Inventory
    service window.
    However some workstations can't store to the inventory db, but DO populate
    eDir ZENworks inventory 'minimal information' but show "Scanner(Warning) :
    027: Unable to read the sequence number from the Workstation object." in
    the Scan Status...
    The Inventory service window shows no attempt by these workstations - it's
    almost as though the scan file is not arriving (though eDir knows/displays
    the scan file name)
    How does the workstation access the scandir in Windows only/middle tier
    environment? Does the scan xml stream get sent to the MT via http and then
    on to the scandir via CIFS?
    Any suggestions/explanations welcome!!
    Many thanks
    David

    David,
    It appears that in the past few days you have not received a response to your
    posting. That concerns us, and has triggered this automated reply.
    Has your problem been resolved? If not, you might try one of the following options:
    - Do a search of our knowledgebase at http://support.novell.com/search/kb_index.jsp
    - Check all of the other support tools and options available at
    http://support.novell.com.
    - You could also try posting your message again. Make sure it is posted in the
    correct newsgroup. (http://support.novell.com/forums)
    Be sure to read the forum FAQ about what to expect in the way of responses:
    http://support.novell.com/forums/faq_general.html
    If this is a reply to a duplicate posting, please ignore and accept our apologies
    and rest assured we will issue a stern reprimand to our posting bot.
    Good luck!
    Your Novell Product Support Forums Team
    http://support.novell.com/forums/

  • 027: Unable to read the sequence number from the Workstation object

    Environment:
    NW 6.5 sp4
    Zen 6.5 sp1
    We had a problem with our inventory server processing .STR files a couple
    months ago - had to turn off inv. policies on workstations while we resolved
    it. Now it's resolved and we're running inventory again. We have a large
    number of workstations reporting this error:
    027: Unable to read the sequence number from the Workstation object
    They have no data in the DB, although they do have minimal scan info, and
    the last scan status date matches that of the sequence number error.
    I've seen this sporadically in the past and have been able to resolve it by
    initiating a full scan on the individual workstation. However, that does
    not seem to be making a difference this time.
    Novell's troubleshooting document basically says see if the server's
    running, and see if there's data for the workstation in the db. Then call
    tech support.
    Any other ideas?
    ~~~Nicoya...

    Jared wrote:
    > Nicoya Helm,
    >
    >> 027: Unable to read the sequence number from the Workstation object
    >
    > I am thinking that this can also mean a previous full scan was not
    > found. Or maybe the STR file name is bad.
    >
    > This information is stored here
    > HKEY_LOCAL_MACHINE\Software\Novell\Workstation
    > Manager\InvScanner\"STRFileName"
    The machines I'm troubleshooting have valid formats for their STR names,
    but that doesn't necessarily mean they are valid STR sequence numbers.
    Does removing the value in the reg key cause the STR file name to be
    recreated?
    >
    > Because imaging is used, it's possible that this information is matching
    > on some machines which could cause problems.
    I've verified that none of our images contain the c:\zenworks history
    folder, and we run a script that cleans up any zenworks items before
    sysprep (i.e. makes sure there's no registration info on the
    workstations, etc). Our imaging scripts also clear any ZIS info to
    avoid any duplication as well, so I don't think imaging would be causing
    this.
    > I believe 6x also has a hist.ini file like 3x inventory does, you might
    > delete the file also.
    Good tip about the hist.ini file, I didn't know about that. It does not
    solve the sequence number error, but it does seem to force a full scan
    of the system and cause the new scan to be stored in the DB, which was
    not happening before.
    Thanks!

  • SSIS The parameterized sql statement yields metadata which does not match the main SQL statement

    Hi all,
    I'm getting the above error when trying to execute a custom lookup.  What I'm trying to do is lookup the minimum promotional price between date ranges.  The SQL executes fine in SSMS
    SELECT refTable.PRICE
    FROM ( SELECT MIN(PRICE) AS PRICE ,
    NAME ,
    ITEMRELATION ,
    FROMDATE ,
    TODATE
    FROM [dbo].[AllCustPrices]
    GROUP BY NAME ,
    ITEMRELATION ,
    FROMDATE ,
    TODATE
    ) [refTable]
    WHERE [refTable].[NAME] = ?
    AND [refTable].[ITEMRELATION] = ?
    AND ? BETWEEN [refTable].[FROMDATE]
    AND [refTable].[TODATE]
    but errors in SSIS?

    Apologies, totally forgot i posted a question on here.  I still haven't resolved this.  full error text is TITLE: Package Validation Error
    Package Validation Error
    ADDITIONAL INFORMATION:
    Error at Data Flow Task [Lookup MIN Price [440]]: The parameterized SQL statement yields metadata which does not match the main SQL statement.
    Error at Data Flow Task [SSIS.Pipeline]: "Lookup MIN Price" failed validation and returned validation status "VS_ISBROKEN".
    Error at Data Flow Task [SSIS.Pipeline]: One or more component failed validation.
    Error at Data Flow Task: There were errors during task validation.
     (Microsoft.DataTransformationServices.VsIntegration)
    BUTTONS:
    OK
    If I take off the MIN function and just put * SSIS is quite happy, it does not like MIN for some reason?
    I'm really stumped with this... checked metadata for third parameter which is W/C Date DT_DBTIMESTAMP which is same as FROMDATE, TODATE where i'm using BETWEEN...

  • HT1941 I need to find my Apple Care warranty number although the warranty has expired - to prove ownership of macbook air

    I need to find my Apple Care warranty number although the warranty has expired - to prove ownership of macbook air?

    You really aren't trying very hard are you?
    Apple Warranty Information
    Review your support and service coverage
    Warranty Information and Support and Service Coverage
    Read the warranty

  • How can find SCN or sequence number on the update operation?

    nedd to recovery database, how can find the SCN or sequence number before the update exection?
    thanks

    nedd to recovery database, how can find the SCN or
    sequence number before the update exection?Sorry - there is something confusing about your question ...
    It seems to me the SCN is the 'system change number', which identifies when a change (table, row, block, flush log buffer to log file, etc) has ocurred.
    You seem to be asking for the SCN associated with an update before the update actually occurred.
    Are you actually asking "How do I perform a point in time recovery to immediately before a specific update?"
    Perhaps you could include a few more details, such as database version. For some reason different versions have different capabilities - that could be helpful here.

  • Generate spool req number for report output list

    Hi,
    How to generate spool request number for report output list while executing the report.Please provide if you have any sample code.
    Thanks and Regards,
    BSR.

    HI srinivas,
    welcome to sdn.
    Exporting the Contents of a Spool Request
    Use
    You want to export the content of a spool request in one of the following ways:
    ·        As a text file to the SAP GUI working directory
    ·        Unconverted or as a table, RTF, or HTML to a directory of your choice
    ·        As a PDF file to a directory of your choice
    Procedure
    Follow the procedure below:
    Exporting to the SAP GUI Working Directory
    If you are exporting large quantities of data, downloading the spool request as a text file to the SAP GUI working directory is a good solution.
    Choose Spool Request ® Forward® Export as Text.
    The entire text is stored in your SAP GUI working directory in ASCII format.
    A file of this type is named using the following pattern:
    .txt
    Example: ABC0000004327.txt
    You require appropriate authorization for this function from your administrator.
    Exporting Unconverted or as a table, RTF, or HTML to a Directory of Your Choice
    With this method of exporting a spool request, the content of the spool request is first displayed, and you then download the content in the format of your choice to a directory of your choice.
           1.      Select the spool request to be exported and choose  Display Contents.
           2.      In the case of SAPscript/Smart Forms documents, activate list display by choosing Goto.
           3.      Choose System ® List ® Save ® Local File.
           4.      Choose one of the available formats and confirm your choice.
           5.      Choose a directory and save the spool request.
    By default, only the first 10 pages of a spool request are saved in a file. You can increase the number of pages to be saved by choosing Goto ® Display Requests ® Settings and making the desired entries in the Display Area group box.
    Exporting as a PDF File
    You want to export the contents of a spool request as a PDF file to a directory of your choice, and print the file as required. The PDF file contains the print data in the format in which it would be output by the printer.
    The following procedure is irrelevant for the printing of PDF-based forms, since a PDF file is already returned with this method. See also Displaying and Printing PDF-Based Forms.
    You also require authorization from your administrator to run this report.
    The PDF file is generated as follows with report RSTXPDFT4:
           1.      Generate a spool request from the document to be printed.
           2.      In transaction SE38, start report rstxpdft4.
           3.      In the displayed window, enter the spool request number and the directory in which the PDF file is to be stored.
    Leave the Download PDF File option selected.
    Choose  Execute.
           4.      In the next window, you can confirm or change the path in which the file is be stored.
    Save your entries.
           5.      The system displays a log from which you can see whether the report was successfully performed.
    You can then open the file from the directory and print it as required.
    Restrictions for Exporting as a PDF File
    ·        The PDF conversion only supports true bar codes for Smart Forms, which were generated with the new bar code technology as of SAP NetWeaver 04. In all other cases, the bar code is only simulated.
    ·        PDF conversion, especially of ABAP lists, is slower and is therefore not suitable for mass printing. However, you can speed up the conversion to PDF using the FASTLISTCONV option in report RSTXPDF3.
    ·        The font selection for ABAP lists is predefined in the PDF converter and cannot be changed.
    For more information about constraints, see SAP Note 323736 in the SAP Service Marketplace
    see this links
    http://help.sap.com/saphelp_40b/helpdata/en/d9/4a98f351ea11d189570000e829fbbd/content.htm
    http://web.mit.edu/SAPR3/docs/webdocs/reports/rpRFprint.html
    regards
    shankar
    reward me if usefull

  • Select from table in group of columns and generate a sequence number

    I have to select data from a table in group of columns and generate a sequence for every group resetting the sequence to start from 1 onwards.
    For example:
    Data:
    Col1 Col2 Col3 Col4
    A NA KA 2009-08-13
    B NA KA 2009-08-13
    C NA KA 2009-08-13
    A NA KA 2009-08-13
    B NA KA 2009-08-13
    A NA KA 2009-08-13
    Expected output from Select Statement:
    Col1 Col2 Col3 Col4 Seq_No
    A NA KA 2009-08-13 1
    A NA KA 2009-08-13 2
    A NA KA 2009-08-13 3
    B NA KA 2009-08-13 1
    B NA KA 2009-08-13 2
    C NA KA 2009-08-13 1
    How can this be possible with a SELECT statement? Is it possible to assign seq numbers for a group of columns and reset it when it changes? In the above example, all columns form the key to generate the seq number
    I know it can be done using Stored procedures and that is how I am doing it now by introducing a temporary table.
    Can anyone help me in this regard? Please let me know if the question is vague to understand!
    Thanks,
    Nachi

    with t as(select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
    union all
    select 'B' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
    union all
    select 'C' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
    union all
    select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
    union all
    select 'B' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
    union all
    select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual)
    select t.*,row_number() over (partition by col1,col2,col3,col4 order by col1,col2,col3,col4) from tYou can replace partition by col1,col2,col3,col4 with only columns that you need for grouping condition
    and also order by you can just do on the column you need.

  • Need SQL statement for this logic....

    Hi,
    I want a SQL statement for updating the following changed last number .
    Cuurently its:
    SELECT * FROM TEST;
    LAST NUMBER     CHANGED LAST NUMBER
    123518          
    12355265     
    123674659     
    9087648970     
    After updating with the required SQL statement table should look like
    LAST NUMBER     CHANGED LAST NUMBER
    123518          0000123518
    12355265     0012355265
    123674659     0123674659
    9087648970     9087648970
    the last number should be appended with ZEROs and the length of changed last number should be 10 always. Hope its clear.
    Appreciate your help.
    Thanks in advance
    Devender

    select last_number, lpad(to_char(last_number), 10 , '0') FROM test

  • How can we generate auto Sequence Number in DFF attribute

    Dear
    We are on r12,rignt now we have an issue to generate sequence number in site Level(DFF) on Supplier window,actully we want
    to generate sequence number on site leve while inserting new record on site level,so how could i achvie this or any way which can help us.
    --thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    You can consider writing a before-insert trigger.
    The trigger generates the next value of the sequence and makes :new.attributen = the sequence value.
    Hope this helps,
    Sandeep Gandhi

  • Need sql statement

    Hi,
    My requirement
    emp_table---table name
    ename empno
    ram 1
    sham 2
    using this select statemnt
    select * from emp_table where empno in (1,1)
    But it shows only one entry that is
    ename empno
    ram 1
    But i wnat to get the output in this format
    ename empno
    ram 1
    ram 1
    becasue i given the empno 1 in two times in the where caluse .. so i need to get the two entries. howto do that.

    Hi,
    Duplicate items count as one item with the IN operator; that is, IN returns TRUE or FALSE, depending on whether the item was found in the list or not. It has no way of telling you how many times the item was found (not that it tries to count them all, anyway).
    The easiest way for you to get the results you want would be to have a table (perhaps a global temporary table, or perhaps just the result set of a sub-query, like "table" t in Karthick's answer) that can have multiple duplicate rows.
    If you have a delimited string (like '1,1,1,1,1') you can generate a result set that has one row for every item in the list. Using the REGEXP_ functions introduced in Oracle 10, this is quite easy.
    Give more specifics about your problem (especially how you are getting the input, whether it is '1', '1,1' or '1,1,1,1,1') to get a more specific answer.

  • Is there a way using core foreach for generating decreasing sequence number

    how to generate decreasing integer number like 20, 19 ... using jstl c:foreach. I've done like :
        <c:foreach var="count" begin="20" end="1" step="-1">
            <c:out value="${count} />
        </c:foreach>
    {code}
    I came to know that we can't use step < 0...

    I came to know that we can't use step < 0...Because the spec says so, thats why. Its probably to do with the fact that the forEach loop is used for multiple things, and using step="-1" would create some strange errors iterating on some things - eg Result Sets
    <c:foreach var="count" begin="1" end="20" >
      <c:out value="${21 - count} />
    </c:foreach>{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to execute a same sequence number for the resulting dynamic rows

    Hi friends,
    I have a sequence and trigger(which will fire id for each and every row insert).
    My scenario is, If i update more than 5 rows at a time means, i need to set a same sequence number example(1) for all the 5 rows.
    Next time, if i update another 5 rows means, then i need to set a same sequence number (i.e) 2 for the next 5 rows.
    So, my updation will be dynamic(may be 2 ,5, 10 rows at a time), But i need to set a same sequence number for one of the column for all the rows that i update.
    Next time, if i update another set of rows means, then i need to set only the next sequence number(i.e) 2(assume that previously updated sequence for the before set of rows is 1) for another set of rows.
    Like that sequence has to update in a sequential manner for more than one set of rows in that column.
    How to achieve it friends.
    example
    id-----------------name
    1-------------------A
    1-------------------B
    1-------------------C
    1-------------------D
    1-------------------E
    2-------------------D
    2-------------------E
    2-------------------F
    2-------------------G
    3-------------------H
    3-------------------IBrgds,
    Mini

    Hi,
    You forgot to mention your version.
    If you want a trigger based solution, then in 10g you need two triggers
    1. A statement level trigger that takes sequence.nextval into a dummy variable that will not be used.
    2. A for each row trigger that uses sequence.currval.
    In 11g you should be able to write a single, composite trigger.
    If you want a solution with no triggers, it could look something like
    SQL> create sequence s start with 10;
    Sequence created.
    SQL>
    SQL> create or replace function s_wrapped
      2  return number
      3  as
      4     v number;
      5  begin
      6     select s.nextval
      7     into v
      8     from dual;
      9
    10     return v;
    11  end s_wrapped;
    12  /
    Function created.
    SQL> create table tab1 as (Select level x, cast(null as number) y from dual connect by level <= 5);
    Table created.
    SQL> update tab1
      2    set y = (select s_wrapped from dual)
      3   where x <= 3;
    3 rows updated.
    SQL> commit
      2  /
    Commit complete.
    SQL> select *from tab1
      2  /
           X          Y
           1         10
           2         10
           3         10
           4
           5
    SQL>Regards
    Peter

  • Sql loader how import a file with save the actual row number in a db field

    Hey,
    how can i save the row number for each row in a file into a database field i the same table with the sql loader (sqlldr)?
    test data:
    john doe
    joe meier
    table:
    name surname line_number_in_file
    john doe 1
    joe meier 2
    and so on ... an if an line is discarded because it is empty for example it should be as this:
    test data:
    john doe
    joe meier
    table:
    name surname line_number_in_file
    john doe 1
    joe meier 3
    thanks for helpinmg and excuse my very bad english :-)
    Message was edited by:
    user527827

    but if one of the when clause faild or some rows are
    rejectet the sequence is not right.Did you read the link?
    "If a record is rejected (that is, it has a format error or causes an Oracle error), the generated sequence numbers are not reshuffled to mask this. If four rows are assigned sequence numbers 10, 12, 14, and 16 in a particular column, and the row with 12 is rejected, the three rows inserted are numbered 10, 14, and 16, not 10, 12, and 14. This allows the sequence of inserts to be preserved despite data errors. When you correct the rejected data and reinsert it, you can manually set the columns to agree with the sequence."
    also if you read multiple lines and they inserted in
    the wrong order the number from the sequence is not
    the line number...what do you mean with "reading multiple lines and wrong order?"

Maybe you are looking for

  • Replace old iPhone 3G with the new  iPhone 4S ?

    I have been having some problems with my old iPhone 3G - bought from outside the US - and want to get it replaced. Does anyone know if Apple will replace this old iPhone 3G with the new  iPhone 4S ?

  • Altered graphics don't display properly

    I am running Lookout 6.2 on a small panel pc. I am using custom graphics which up to now display properly. I needed to make a couple of changes to the graphic, which was done on another computer then the exiting graphic was replaced with the new one,

  • Only my MBP Drops L2TP VPN Connection after 60 Seconds

    My Old G4 Laptop and my Dual G5 Tower both connect to my office via L2TP VPN without an issue. with the exact same settings exported over to my mac book pro, the connection drops after exactly 60 seconds. I've tried both wireless and wired connection

  • IPhone 3G S in Finland

    Sonera have started pre-order sales for the iPhone 3G S. All pretty much sold out, only white 16GB left. Does anyone know when 32GB version will be back onsale?

  • Cannot make a video call via Skype WiFi?

    I am trying to make a video call and having trouble doing it. I have signed in and added Skype WIFI to my account but I still cannot make a call. It keeps telling me that I need Skype Credit when it should be free. Can youhelp me with this problem? P