Resetting column sequence in SQL

Hi All, I have a simple requirement but I cant think of the solution...
to simplify the problem, I have a master field and its child field, I need another column to bring back a sequence starting from 1...n and resetting itself back to 1 when a new master records starts eg...
Master_id child_id seq
200 100 1
200 105 2
200 120 3
205 151 1
205 159 2
300 170 1
ect
If a solution can be done using purely SQL, that would be ideal
also please note there is no arithmetic relationshp between the master rec and its child rec
Kind Regards
Satnam

SQL> select m,c,row_number() over(partition by m order by rownum) r from sqn;
      M       C       R
    200     100       1
    200     105       2
    200     120       3
    205     150       1
    205     160       2
    210     300       1
    210     305       2
7 rows selected.
"sorry Avinash, not seen your post you already suggested the same"Message was edited by:
Jameel

Similar Messages

  • Resetting the Sequence operator in OWB

    I have a Sequence operator for one column in the mapping. It correctly increments by 1 for each row (nextval) . I have done some testing and now I want to reset the next increments. Like I want to start from 1 instead of some 588 like that which is result of my testing.
    How to reset the sequence operator so that it starts from 1 again.
    Thanks

    Hi Tom,
    First you should create a store procedure that resets the last_number of a specified sequence w/ parameters sequence_name and start value. Then just call the stored proc from OWB to reset your sequence.
    --stored proc ex:
    create or replace PROCEDURE reset_sequence (
    seq_name IN VARCHAR2, startvalue IN PLS_INTEGER) AS
    cval INTEGER;
    inc_by VARCHAR2(25);
    BEGIN
    EXECUTE IMMEDIATE 'ALTER SEQUENCE ' ||seq_name||' MINVALUE 0';
    EXECUTE IMMEDIATE 'SELECT ' ||seq_name ||'.NEXTVAL FROM dual'
    INTO cval;
    cval := cval - startvalue + 1;
    IF cval < 0 THEN
    inc_by := ' INCREMENT BY ';
    cval:= ABS(cval);
    ELSE
    inc_by := ' INCREMENT BY -';
    END IF;
    EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || seq_name || inc_by ||
    cval;
    EXECUTE IMMEDIATE 'SELECT ' ||seq_name ||'.NEXTVAL FROM dual'
    INTO cval;
    EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || seq_name ||
    ' INCREMENT BY 1';
    END reset_sequence;
    --calling stored proc ex:
    --sequence name is employees_seq; reset value to 1
    DECLARE
    SEQ_NAME VARCHAR2(200);
    STARTVALUE PLS_INTEGER;
    BEGIN
    SEQ_NAME := 'EMPLOYEES_SEQ';
    STARTVALUE := 1;
    RESET_SEQUENCE(
    SEQ_NAME => SEQ_NAME,
    STARTVALUE => STARTVALUE
    END;
    Hope it works! Cheers!=)
    Regards,
    Carlo

  • Changing Column Sequence

    Hi,
    How to change the column sequence with out recreating a table?
    Exapmle :
    SQL> desc emp
    Name Null? Type
    EMPNO NOT NULL NUMBER(4)
    ENAME VARCHAR2(10)
    JOB VARCHAR2(9)
    MGR NUMBER(4)
    HIREDATE DATE
    SAL NUMBER(7,2)
    COMM NUMBER(7,2)
    DEPTNO NUMBER(2)
    SQL>
    Expected:
    SQL> desc emp
    Name Null? Type
    EMPNO NOT NULL NUMBER(4)
    DEPTNO NUMBER(2)
    ENAME VARCHAR2(10)
    JOB VARCHAR2(9)
    MGR NUMBER(4)
    HIREDATE DATE
    SAL NUMBER(7,2)
    COMM NUMBER(7,2)
    SQL>
    Regards

    I would not even dream of doing such thing on a production database :(How about DBMS_REDEFINITION? It allows us to do redefinition stuff online.
    A little example:
    SQL> create table emp
      2    (empno number primary key,
      3     empname varchar2(30),
      4     deptno number,
      5     address varchar2(50));
    Table created.
    SQL> insert into emp
      2  select rownum,
      3         'EMPLOYEE ' || rownum,
      4         mod(rownum, 10) + 1,
      5         'ADDRESS ' || rownum
      6    from dual
      7  connect by level <= 100;
    100 rows created.
    SQL> commit;
    Commit complete.
    SQL> desc emp
    Name                                                  Null?    Type
    EMPNO                                                 NOT NULL NUMBER
    EMPNAME                                                        VARCHAR2(30)
    DEPTNO                                                         NUMBER
    ADDRESS                                                        VARCHAR2(50)
    SQL> exec dbms_redefinition.can_redef_table(user ,'EMP', dbms_redefinition.cons_use_pk)
    PL/SQL procedure successfully completed.
    SQL> create table emp_new
      2    (empno number primary key,
      3     deptno number,
      4     empname varchar2(30),
      5     address varchar2(50));
    Table created.
    SQL> exec dbms_redefinition.start_redef_table(user, 'EMP', 'EMP_NEW', options_flag => dbms_redefinition.cons_use_pk)
    PL/SQL procedure successfully completed.
    SQL> exec dbms_redefinition.finish_redef_table(user, 'EMP', 'EMP_NEW')
    PL/SQL procedure successfully completed.
    SQL> desc emp
    Name                                                  Null?    Type
    EMPNO                                                 NOT NULL NUMBER
    DEPTNO                                                         NUMBER
    EMPNAME                                                        VARCHAR2(30)
    ADDRESS                                                        VARCHAR2(50)
    SQL> set linesize 1000
    SQL>
    SQL> select * from emp where rownum <= 5;
         EMPNO     DEPTNO EMPNAME                        ADDRESS
             1          2 EMPLOYEE 1                     ADDRESS 1
             2          3 EMPLOYEE 2                     ADDRESS 2
             3          4 EMPLOYEE 3                     ADDRESS 3
             4          5 EMPLOYEE 4                     ADDRESS 4
             5          6 EMPLOYEE 5                     ADDRESS 5
    SQL> drop table emp_new;
    Table dropped.Regards.

  • Reset a Sequence at Midnight on New Years Eve

    Hi,
    How can I reset a sequence generator at midnight on New Year’s Eve?
    Using SQL script and SQL-Plus, I know how to set the sequence increment to a negative number large enough to return the “Next Val” to 1 (then re-set increment to 1).
    But, ‘need help to understand how to cause this action to happen automatically. I’m thinking along the lines of a trigger or stored procedure.
    Thank you

    I have some thing to add to John's idea.
    You could use a negative INCREMENT BY. Typically most sequences are created without reference to minvalue. Oracle sets a value for the minvalue. If the original sequence was not created with 'nominvalue' then, the error like the following will show up.
    ORA-08004: sequence sequencename.NEXTVAL goes below MINVALUE and cannot be instantiated
    Hence you are better off doing the following in order.
    1. Alter the sequence and set the appropriate negative INCREMENT BY and also specify 'nominvalue'.
    2. Use a select statement to get the next value.
    3. Alter the sequence a second time to set INCREMENT BY to the original value.
    Some one could put this in a script and run it only once.

  • How to export data with column headers in sql server 2008 with bcp command?

    Hi all,
    I want know "how to export data with column headers in sql server 2008 with bcp command", I know how to import data with import and export wizard. when i
    am trying to import data with bcp command data has been copied but column names are not came.
    I am using the below query:-
    EXEC master..xp_cmdshell
    'BCP "SELECT  * FROM   [tempdb].[dbo].[VBAS_ErrorLog] " QUERYOUT "D:\Temp\SQLServer.log" -c -t , -T -S SERVER-A'
    Thanks,
    SAAD.

    Hi All,
    I have done as per your suggestion but here i have face the below problem, in print statment it give correct query, in EXEC ( EXEC master..xp_cmdshell @BCPCMD) it was displayed error message like below
    DECLARE @BCPCMD
    nvarchar(4000)
    DECLARE @BCPCMD1
    nvarchar(4000)
    DECLARE @BCPCMD2
    nvarchar(4000)
    DECLARE @SQLEXPRESS
    varchar(50)
    DECLARE @filepath
    nvarchar(150),@SQLServer
    varchar(50)
    SET @filepath
    = N'"D:\Temp\LDH_SQLErrorlog_'+CAST(YEAR(GETDATE())
    as varchar(4))
    +RIGHT('00'+CAST(MONTH(GETDATE())
    as varchar(2)),2)
    +RIGHT('00'+CAST(DAY(GETDATE())
    as varchar(2)),2)+'.log" '
    Set @SQLServer
    =(SELECT
    @@SERVERNAME)
    SELECT @BCPCMD1
    = '''BCP "SELECT 
    * FROM   [tempdb].[dbo].[wErrorLog] " QUERYOUT '
    SELECT @BCPCMD2
    = '-c -t , -T -S '
    + @SQLServer + 
    SET @BCPCMD
    = @BCPCMD1+ @filepath 
    + @BCPCMD2
    Print @BCPCMD
    -- Print out below
    'BCP "SELECT 
    * FROM   [tempdb].[dbo].[wErrorLog] " QUERYOUT "D:\Temp\LDH_SQLErrorlog_20130313.log" -c -t , -T -S servername'
    EXEC
    master..xp_cmdshell
    @BCPCMD
      ''BCP' is not recognized as an internal or external command,
    operable program or batch file.
    NULL
    if i copy the print ourt put like below and excecute the CMD it was working fine, could you please suggest me what is the problem in above query.
    EXEC
    master..xp_cmdshell
    'BCP "SELECT  * FROM  
    [tempdb].[dbo].[wErrorLog] " QUERYOUT "D:\Temp\LDH_SQLErrorlog_20130313.log" -c -t , -T -S servername '
    Thanks, SAAD.

  • SQL Query - The number of columns specified in "SQL Query" does not match t

    I am creating new UDM for tablespace alert, below is my query,however its failing with error
    SQL Query - The number of columns specified in "SQL Query" does not match the value specified in "SQL Query Output"
    I selected Metric type is number
    SQL Query Format : Two columns
    Query:
    SELECT d.tablespace_name,round(((a.bytes - NVL(f.bytes,0))*100/a.maxbytes),2)
    used_pct FROM sys.dba_tablespaces d,(select tablespace_name, sum(bytes) bytes, sum(greatest(maxbytes,bytes)) maxbytes from sys.dba_data_files group by tablespace_name) a,(select tablespace_name, sum(bytes) bytes from sys.dba_free_space group by tablespace_name) f
    WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
    AND NOT (d.extent_management = 'LOCAL' AND d.contents = 'TEMPORARY');
    Any clues why i am getting error.

    SQL> SELECT d.tablespace_name,round(((a.bytes - NVL(f.bytes,0))*100/a.maxbytes),2) used_pct
    2 FROM sys.dba_tablespaces d,(select tablespace_name, sum(bytes) bytes, sum(greatest(maxbytes,bytes)) maxbytes from sys.dba_data_files group by tablespace_name) a,(select tablespace_name, sum(bytes) bytes from sys.dba_free_space group by tablespace_name) f
    3 WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
    4 AND NOT (d.extent_management = 'LOCAL' AND d.contents = 'TEMPORARY');
    TABLESPACE_NAME USED_PCT
    MGMT_TABLESPACE .82
    SYSAUX 1.52
    UNDOTBS1 .32
    RMAN .02
    CORRUPT_TS 10.63
    USERS 0
    SYSTEM 2.26
    MGMT_ECM_DEPOT_TS .04
    MGMT_AD4J_TS 0

  • Apex 4.0 display image item :BLOB column returned by SQL statement

    Hello
    I'm creating an display image item in apex 4.0. The source is a BLOB column returned by SQL statement.
    When I'm issuing an sql statement like this:
       select lado.blob_content
       from   large_documents      lado
       ,        large_doc_headers    ladh
       where lado.ladh_nr = ladh.nr
       more criteriait works fine.
    When I create a function inside a package with the same query (in a cursor)
    function get_image(some parameters in) return blob
    Following in apex by:
    select get_image(some parameters) from dualI get a
    ORA-06502: PL/SQL: numeric or value error: character string buffer too smallAnybody any idea why this does not work?
    Regards Erik

    Hi Eric,
    the environment assumes varchar2-output by default, which will be limited to 32767 characters and may have problems with binary formats. You could define a blob-variable to select the value into.
    DECLARE
      l_blob BLOB;
    BEGIN
      SELECT get_image(some parameters)
        INTO l_blob
        FROM dual;
    END;
    /If you expect the BLOB-Content to be text (you should consider CLOB then), you may use UTL_RAW.CAST_TO_VARCHAR2 to convert the content. If your object is larger than the maximum varchar2 size, or you want to convert BLOB to CLOB, you might be interested in some converter like described here: {message:id=559749}
    Hope this is what you were searching for.
    -Udo

  • JDBC receiver error:ORA-06550: line 1, column 7: PL/SQL

    Hi,
    I am geting this error in JDBC Receiver adapter. when  i call the stored procedure.
    Last week it works fine.But i today i faced this error.
    Error Unable to execute statement for table or stored procedure. 'spec_utilities.SP_DELETE(Structure 'unassign') due to java.sql.SQLException: ORA-06550: line 1, column 7: PLS-00201: identifier 'SPEC_UTILITIES.SP_DELETE' must be declared ORA-06550: line 1, column 7: PL/SQL: Statement ignored 
    Error JDBC message processing failed; reason Error processing request in sax parser: Error when executing statement for table/stored proc. 'spec_utilities.SP_DELETE(structure 'unassign'): java.sql.SQLException: ORA-06550: line 1, column 7: PLS-00201: identifier 'SPEC_UTILITIES.SP_DELETE' must be declared ORA-06550: line 1, column 7: PL/SQL: Statement ignored 
    Error MP: Exception caught with cause com.sap.aii.af.ra.ms.api.RecoverableException: Error processing request in sax parser: Error when executing statement for table/stored proc. 'spec_utilities.SP_DELETE (structure 'unassign'): java.sql.SQLException: ORA-06550: line 1, column 7: PLS-00201: identifier 'SPEC_UTILITIES.SP_DELETE'must be declared ORA-06550: line 1, column 7: PL/SQL: Statement ignored 
    My stored procedure structure is:
    <unassign>
    <SP_DELETE action="EXECUTE">
      <table>spec_utilities.SP_DELETE</table>
      <PAE isInput="true" type="VARCHAR">447848</pAEIC>
      <SKEY isInput="true" type="NUMERIC">4548545</pSKEY>
      <USER isOutput="true" type="VARCHAR" />
      <UNIT  isOutput="true" type="VARCHAR" />
      <EMAIL isOutput="true" type="VARCHAR" />
      </SP_DELETE>
      </unassign>
    Can you suggest me where is this error.
    Thank you
    Sateesh

    Hi,
       if it was executing fine earlier then check the below
    1. user permissions level at the database..whether it has access to the required SP or not..
    2. Check if there was any change in the SP..which has not been communicated...
    HTH
    Rajesh

  • Display column name in sql*plus

    Hi,
    How to display full column name?
    there are n no of tables, i have to query the tables but i want the column name to be displayed fully. for example
    SQL> desc control
    Name Null? Type
    CODE NOT NULL VARCHAR2(255)
    LOAD_PERIOD_START_DATETIME DATE
    SQL> select code,load_period_start_datetime from control;
    CODE
    LOAD_PERIOD
    AAA
    01-AUG-2007
    SQL> col load_period_start_datetime format a30
    SQL> /
    CODE
    LOAD_PERIOD_START_DATETIME
    AAA
    01-AUG-2007
    SQL>
    As it is only one column i can set with 'col <column_name> format a 30'
    if there are n no of coumns from n no tables then how do i get the full column name?
    Please help me.
    Thanks

    Hi,
    you can get all the column's for a TABLE from all_tab_columns this VIEW, why dont you write as script which will generate the commands.
    Could you please tell us why do you want to display the compete COLUMN NAME in SQL plus.
    Thanks

  • What is LAST_ACTIVE_TIME  column in v$sql view

    Hi,
    I am using Oracle 10.2.0.3 version.
    I have a doubt in v$sql view.
    What is the meaning of LAST_ACTIVE_TIME column in v$sql view?
    I presume it is the latest time when this SQL was executed in the database. M I right?
    As per Oracle docs it's definition is as below.
    LASTACTIVE_TIME - Time at which the query plan was last active ._
    Thanks in advance.
    Best Regards,
    oratest

    Dear oratest,
    Please see the following link;
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/dynviews_2113.htm#REFRN30246
    +"+
    +LAST_ACTIVE_TIME+
    +DATE+
    +Time at which the query plan was last active+
    +"+
    That is the time that the "query plan" active period. I think this could be as what you have said.
    Regards.
    Ogan
    Edited by: Ogan Ozdogan on 30.Ağu.2010 22:21

  • How to show the VALUE as the Column Header using SQL query?

    Hi
    I have a requirement to show the picked value as the column header using SQL query.
    Example:
    ======
    SELECT EMPNO FROM EMP
    WHERE EMPNO=7934;
    Result Should be:
    7934
    7934

    I have a requirement to show the picked value as the column header using SQL query.In sql*plus you can do
    SQL> set verify on
    SQL> def e =  7934
    old: SELECT empno "&&e"  FROM emp  WHERE empno = &&e
    new: SELECT empno "7934"  FROM emp  WHERE empno = 7934
    SQL> SELECT empno "7934"  FROM emp  WHERE empno = 7934
          7934
          7934
    1 row selected.

  • Resetting the password in sql query

    Hi
        Am getting error in once ecc6 Installation over i went for 000client to configure post installation steps but 000 client default  password is not going .Can you please  help me sortout the issue.How to reset sap* password in sql database.
    Regards,
    K.Ramamoorthy

    Just do a search on this forum where you will find multiple threads to reset password of SAP*. Please do some research before posting question here.
    Read Rule of Engagement...
    Thanks
    Sunny

  • SAP Query column sequence for Totalling fields

    Hi all
    We have a custom query in 4.6C in which the output displays only the total of Totaling fields maintained in the sequence Amount > Withholding Tax > Net Amount  (all currency related fields).
    However after upgrade to ECC 6.0, we notice that we are no longer able to sequence the output list to Amount > Withholding Tax > Net Amount
    Though we maintain the Totaling fields in the sequence Amount > Withholding Tax > Net Amount , the list is generated in the sequence Amount > Net Amount > Withholding Tax  and the sequence is changed accordingly in the Basic List after a test display of the Output.
    Is anyone aware if the sequencing control is removed in ECC based for the Totaling Fields?
    Kindly help share.
    Thanks in Advance
    Vinodh S

    Hi Faheem,
    After changing the column sequence i clicked configuration button and click save button and again clicked Configuration and selected Administrative button there i can see the column sequence change and clicked activate button .now if i close and reopen the transaction my column sequence is same with out changes that i made .
    you asked me to save the changes where i can do exactly ? Could you please tell in briefway from scratch so that i can trace where i done mistake.
    Thanking you in advance.
    Regards,
    narasimha.

  • Table column sequence

    Hello WD-Experts,
    I have bind a table into WD dynpro using table UI-Element. The data will shown perfectly.
    My issue: I like to change the sequence of the different columns.
    Example:
    col1 col2 col3
    col3 col1 col2
    Changing the columns by track and drop is just visible in the ROOTUIELEMENTCONTAINER, but not in the layout itself.
    Has anybody an idea how i can change the column sequence?
    Thank you very much in advance.
    regards
    Axel

    Hi Axel,
    We can change the sequence of table column. For this Chose the Edit Mode for your view. Navigate to your Table UI element. Click on it so it shows you all the Columns. Now select a column u wish to move and just right click on that . u will see a option called up and down . With this u ws will move ur column in upper and downward direction. So by this u can change the sequence of any column in ur table.
    Regards
    Manoj kumar

  • WDA Table: Getting column sequence as it really is for the user

    Hi Folks!
    I need the current sequence of table columns as it really is for the user. He may have personalized his table: he may have removed some columns and may have changed the sequence of some columns. And this is what I need to know.
    But unfortunately so far I could only get the column sequence as it is defined in the View Layout using this coding:
      DATA lo_table TYPE REF TO cl_wd_table.
      DATA lo_table_columns TYPE cl_wd_table_column=>tt_table_column.
      DATA lo_table_column TYPE REF TO cl_wd_table_column.
      DATA lt_abstr_table_columns TYPE STANDARD TABLE OF REF TO cl_wd_abstr_table_column.
      DATA ls_abstr_table_column TYPE REF TO cl_wd_abstr_table_column.
      lo_table ?= wd_this->mv_view->get_element( 'RLTAB' ).
      lt_abstr_table_columns = lo_table->get_grouped_columns( ).
      LOOP AT lt_abstr_table_columns INTO ls_abstr_table_column.
        lo_table_column ?= ls_abstr_table_column.
      ENDLOOP.
    Thus: How to get the table column information as they are currently outside in the reald user world (only visible columns in the displayed sequence).
    Background: I need this information, because I want to export the table to excel and/or print it in exactly the state the user currently sees it (in personalized state).
    Thanx and points for any useful hints - ideally coding snippets!
    Regards,
    Volker

    Hi both!
    1) I have searched the framework like a mad man an dI think I will dream of all these IF_WD... and CL_WD... most having the useful information in protected or even private areas. A fact I cannot really understand.
    2) Although some kind of "criminal" I trewd to go the way in the second answer by creating child classes for cl_wd_abstr_table_column and cl_wd_table_column both containing the protected attribute PERSINDEX I need.
    I added a public class get_pers_index to the child classes. So far so good.
    But when I try to type-cast SAP standard instance to my instance vairables I get type-cast errors in both cases.
    Any ideas?
    Here's the relevant coding snippet:
    DATA lo_table TYPE REF TO cl_wd_table.
      DATA lo_table_columns TYPE cl_wd_table_column=>tt_table_column.
      DATA lo_table_column TYPE REF TO cl_wd_table_column.
      DATA lo_table_column2 TYPE REF TO /rand/cl_wd_table_column.
      DATA lt_abstr_table_columns TYPE STANDARD TABLE OF REF TO cl_wd_abstr_table_column.
      DATA ls_abstr_table_column TYPE REF TO cl_wd_abstr_table_column.
      data lo_abstr_table_column type ref to /rand/cl_wd_abstr_table_column.
      data lv_pers_index type i.
      lo_table ?= wd_this->mv_view->get_element( 'RLTAB' ).
      lt_abstr_table_columns = lo_table->get_grouped_columns( ).
      LOOP AT lt_abstr_table_columns INTO ls_abstr_table_column.
        lo_table_column ?= ls_abstr_table_column.                 "this casting is successful
        lo_table_column2 ?= lo_table_column.                      "this casting throws type-cast exception
        lv_pers_index = lo_table_column2->get_pers_index( ).
        lo_abstr_table_column ?= ls_abstr_table_column.           "this casting throws type-cast exception, too
        lv_pers_index = lo_abstr_table_column->get_pers_index( ).
      ENDLOOP.
    Any help very welcome.
    Thanx and Regards,
    Volker

Maybe you are looking for

  • Wireless LED/Touchpad

    Is there a way to re-enable my wireless card after it has been disabled using a touch pad without rebooting? I have an HP laptop and the wireless card has this light/touch button which turns orange when the card is down and blue when it's up. Under t

  • Database refresh from db A to db B and changing the name from B to A

    We have two databases A and B, both running on HP-UX environment on different servers. both of 10.2.0.1.0 version. Now, database A is production with 75.5 GB of data. database B is just skeloton with no user tablespaces. 1.Our requirement is to refre

  • Nexus 9000 and ACL

    We are replacing all of the switches in the network with two Nexus 93128 switches.  We have 4 VLANS  and need to add security between the MGNT VLAN, IIS Server VLAN, application VLAN and DB VLAN. This is all internal use, no external users.  The prob

  • Reversing page order displayed

    Is there a way to reverse the page order of a document without using the thumbnails to drag and position one at a time or having to rescan the documents? For example, I have a 200 page scanned image document. Its a correspondence file with the most r

  • Song data changes and album art not updating

    I've been using itunes for a couple of years pretty smoothly, but lately (past 2 months or so), I've noticed a degradation in the performance. When I go to update the album art or other song info for a whole ablum at a time, maybe only 6 tracks are u