Function to Generate a Sequential incremental Number by Group of Records

I have a table with several columns and I need to compute a sequential number by group of records
FIELD1 FIELD2 FIELD3 FIELD4 FIELD5
AAAAAA A1 JOHN 01/01/61 NULL
AAAAAA A3 PAUL 01/01/71 NULL
AAAAAA A3 MARY 01/01/02 NULL
AAAAAA A4 CARL 01/01/04 NULL
BBBBBB A1 MARK 01/01/60 NULL
BBBBBB A2 ALLY 01/01/60 NULL
BBBBBB A5 JUAN 01/01/04 NULL
I need to compute FIELD5. This is defined as a number field.
Records where FIELD2 = A1 the default value for FIELD5 is zero.
I am grouping records by FIELD1, FIELD4
The value for FIELD5 will be assigned from 1 TO 1+N
For this sample, the resulting records will look like this;
FIELD1 FIELD2 FIELD3 FIELD4 FIELD5
AAAAAA A1 JOHN 01/01/61 0
AAAAAA A3 PAUL 01/01/71 1
AAAAAA A3 MARY 01/01/02 2
AAAAAA A4 CARL 01/01/04 3
BBBBBB A1 MARK 01/01/60 0
BBBBBB A2 ALLY 01/01/60 1
BBBBBB A5 JUAN 01/01/04 2
Question: Can I create a function that loops through the table and compute the value for FIELD5?

I like this idea, but because F2 must always have the value of ZERO I decided to create a procedure to calculate the value for F5.
My procedure is below;
The issue I am having is that I am not sure;
1) When/where to set/reset the counter ?
2) Determine in the inner loop if I am still whithin the same group of records ?
3) When to update the new found value for MBR_MBR_DEP_CD field ?
CREATE OR REPLACE PROCEDURE LC_BUILD_DEP_CODE_PROC IS
V_ALT_ID VARCHAR2(30);
V_EMPLOYEE_SSN VARCHAR2(9);
--RELATIONSHIP_CD VARCHAR2(2);
--MBR_DOB         DATE;
V_ALT_ID_2 VARCHAR2(30);
V_EMPLOYEE_SSN_2 VARCHAR2(9);
V_MBR_DEP_CD_2 NUMBER(2,0);
V_MBR_DEP_COUNTER INTEGER :=0;
/* FIRST CURSOR */
CURSOR GET_FAM_ALT_ID IS
SELECT FAM.ALT_ID,
FAM.EMPLOYEE_SSN
FROM TPA_MBR_STG FAM
WHERE FAM.RELATIONSHIP_CD = '18'
ORDER BY FAM.ALT_ID;
/* SECOND CURSOR */
CURSOR GET_MBR_DEP_CD IS
SELECT MBR.ALT_ID,
MBR.EMPLOYEE_SSN,
MBR.MBR_DOB,
MBR.MBR_DEP_CD
FROM TPA_MBR_STG MBR
--WHERE MBR.ALT_ID = V_ALT_ID
--AND   MBR.EMPLOYEE_SSN = V_EMPLOYEE_SSN
WHERE MBR.RELATIONSHIP_CD <> '18'
ORDER BY MBR.ALT_ID, MBR.MBR_DOB;
BEGIN
-- OPEN FIRST CURSOR
OPEN GET_FAM_ALT_ID;
LOOP
FETCH GET_FAM_ALT_ID INTO V_ALT_ID,
V_EMPLOYEE_SSN;
OPEN GET_MBR_DEP_CD;
FETCH GET_MBR_DEP_CD INTO V_ALT_ID_2,
V_EMPLOYEE_SSN_2;
IF V_ALT_ID_2 = V_ALT_ID
AND V_EMPLOYEE_SSN_2 = V_EMPLOYEE_SSN
THEN
LOOP
V_MBR_DEP_COUNTER = V_MBR_DEP_COUNTER + 1;
UPDATE TPA_MBR_STG
SET MBR_MBR_DEP_CD = V_MBR_DEP_COUNTER;
END LOOP;
END IF;
CLOSE GET_MBR_DEP_CD;
V_MBR_DEP_COUNTER :=0;
END LOOP;
CLOSE GET_FAM_ALT_ID;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001,'AN ERROR WAS ENCOUNTERED - '||SQLCODE||' -
ERROR- '||SQLERRM);
END LC_BUILD_DEP_CODE_PROC;

Similar Messages

  • How to generate the batch number for group of records

    Hi All,
    requirement: i have created one sequence to generate the batch numbers.i have to get the nextval of the sequence for batch number (this column is not part of the table. its dynamic column/alias column) only for every 50000 records of a table.
    detail description:
    for example, if i have 10 records in a table. And, if i want to generate the batch number for every 2 records, then the output should look like belo.
    batch-number qty
    1 10
    1 20
    2 30
    2 40
    3 50
    3 60
    4 70
    4 80
    5 90
    5 100
    Please help me on this. Please let me know if you need more info.

    one more update on the above request. this has to be done using SQL query only.

  • OBIEE only getting limited number of group membership records

    Hey everyone,
    I'm seeing some strange behavior with the group membership functionality of OBIEE. Right now we're on version 10.1.3.2 and we've implemented SSO and we setup a query against LDAP (AD) to get user group information similar to the way Venkat's blog demonstrates:
    http://oraclebizint.wordpress.com/2007/10/12/oracle-bi-ee-101332-and-oid-user-and-group-phase-2/
    At first glance, everything was working smoothly, however, on second glance, I noticed that on users who were part of lots of groups (i.e. 80 groups), not all of their membership information was getting into OBIEE. On my test user, who was part of only 10 groups, I ran a test in which I only gave access to the Answers module to a person from the 10th group. When I logged into OBIEE as my test user, I was able to access answers.
    On my second test user, who had 80 groups, I set access to answers for the 75th and 80th groups (both different tests). Neither test allowed this user to access answers. However, when I choose the 5th group returned, the user was quickly able to see and access answers.
    When I test out the call to the Oracle function in the Admin tool, I see all the groups returned there.
    These strange results lead me to believe that there is only so many group membership records that OBIEE can receive. Is that true? Has anyone seen this before? Did I forget to set something appropriately?
    Thanks everyone for your help!
    -Joe

    Hey,
    Sorry about the delay in getting back to you, I was slammed with some work right before the Holiday. Anyway, below is the sample code and an example of it's usage. be sure to replace the <BASE DN>, <LDAP HOST>. <LDAP USER>, and <LDAP PASSWORD> with the appropriate values for your situation.
    Also, you'll need to create the "ARRAY" datatype like in Venkat's blog.
    Best of luck!
    -Joe
    select * from table(getusergroup(‘Jbertram’));
    create or replace FUNCTION GETUSERGROUP(Username in Varchar2) RETURN ARRAY PIPELINED AS
    -- Adjust as necessary.
    l_retval pls_integer;
    l_session dbms_ldap.session;
    l_attrs dbms_ldap.string_collection;
    l_message dbms_ldap.message;
    l_entry dbms_ldap.message;
    l_attr_name varchar2(256);
    l_ber_element dbms_ldap.ber_element;
    l_vals dbms_ldap.string_collection;
    l_raw dbms_ldap.binval_collection;
    l_ldap_base varchar2(256) := '<BASE DN>';
    l_filter varchar2(100) := '(&(cn='||Username||'))';
    l_ldap_host varchar2(100) := '<LDAP HOST>';
    l_ldap_port number := 389;
    l_ldap_user varchar2(100) := '<LDAP USER>';
    l_ldap_passwd varchar2(100):= '<LDAP PASSWORD>';
    l_result varchar2(100);
    begin
    -- Choose to raise exceptions.
    dbms_ldap.use_exception := true;
    dbms_ldap.utf8_conversion := false;
    -- Connect to the LDAP server.
    l_session := dbms_ldap.init(hostname => l_ldap_host, portnum => l_ldap_port);
    l_retval := dbms_ldap.simple_bind_s(ld => l_session, dn => l_ldap_user, passwd => l_ldap_passwd);
    -- Get all attributes
    l_attrs(1) := 'memberOf'; -- retrieve all attributes
    --l_attrs(2) := 'cn';
    l_retval := dbms_ldap.search_s(ld => l_session
    ,base => l_ldap_base
    ,scope => dbms_ldap.scope_subtree
    ,filter => l_filter
    ,attrs => l_attrs
    ,attronly => 0
    ,res => l_message);
    if dbms_ldap.count_entries(ld => l_session, msg => l_message) > 0
    then
    -- Get all the entries returned by our search.
    l_entry := dbms_ldap.first_entry(ld => l_session, msg => l_message);
    <<entry_loop>>
    while l_entry is not null
    loop
    -- Get all the attributes for this entry.
    dbms_output.put_line('---------------------------------------');
    l_attr_name := dbms_ldap.first_attribute(ld => l_session
    ,ldapentry => l_entry
    ,ber_elem => l_ber_element);
    <<attributes_loop>>
    while l_attr_name is not null
    loop
    -- Get all the values for this attribute.
    l_vals := dbms_ldap.get_values(ld => l_session, ldapentry => l_entry, attr => l_attr_name);
    <<values_loop>>
    for i in l_vals.first .. l_vals.last
    loop
    dbms_output.put_line(substr(l_vals(i),4,instr(l_vals(i),',')-4));
    PIPE ROW(substr(l_vals(i),4,instr(l_vals(i),',')-4));
    end loop values_loop;
    l_attr_name := dbms_ldap.next_attribute(ld => l_session
    ,ldapentry => l_entry
    ,ber_elem => l_ber_element);
    end loop attibutes_loop;
    l_entry := dbms_ldap.next_entry(ld => l_session, msg => l_entry);
    end loop entry_loop;
    end if;
    -- Disconnect from the LDAP server.
    l_retval := dbms_ldap.unbind_s(ld => l_session);
    --dbms_output.put_line('L_RETVAL: ' || l_retval);
    end;

  • A function for generate a actual tree select

    hi,
    i tried to write a function that generate a actual tree select and i think this is very helpful
    Create Or Replace Function Generate_Tree_Fun(p_Path Varchar2,
    p_Delimiter Varchar2,
    p_Level Integer,
    p_Tbl_Name Varchar2,
    p_Parent_Field Varchar2,
    p_Child_Filed Varchar2)
    Return Varchar2 Is
    v_Txt Varchar2(5000) := '';
    v_Row_Num Number;
    v_Max_Count Number;
    v_Child Varchar2(100);
    v_Parent Varchar2(100);
    v_Level Number := p_Level;
    Begin
    If (v_Level = 1) Then
    Return '';
    End If;
    If (v_Level > 1) Then
    v_Txt := '|' || Lpad('_', 6, '_');
    End If;
    If (v_Level > 2) Then
    For i In 1 .. p_Level - 2 Loop
    v_Level := v_Level - 1;
    v_Child := Ltrim(Regexp_Replace(p_Path,
    Ltrim(Replace(p_Path,
    p_Delimiter,
    ')(' || p_Delimiter) || ')',
    '\' || v_Level),
    p_Delimiter);
    Execute Immediate 'Select t.' || p_Parent_Field || '
    From ' || p_Tbl_Name || ' t
    Where t.' || p_Child_Filed || ' = :1'
    Into v_Parent
    Using v_Child;
    Execute Immediate 'Select a, b From (Select Row_Number() Over(Order By t.' ||
    p_Child_Filed ||
    ') a,Count(1) Over(Partition By t.' ||
    p_Parent_Field || ') b,t.' || p_Child_Filed ||
    ',t.' || p_Parent_Field || ' From ' || p_Tbl_Name ||
    ' t Where t.' || p_Parent_Field ||
    ' = :1 Order By t.' || p_Child_Filed || ') Where ' ||
    p_Child_Filed || ' = :2 '
    Into v_Row_Num, v_Max_Count
    Using v_Parent, v_Child;
    If (v_Row_Num <> v_Max_Count) Then
    v_Txt := Rpad('|', 20, ' ') || v_Txt;
    Else
    v_Txt := Rpad(' ', 20, ' ') || v_Txt;
    End If;
    End Loop;
    End If;
    Return v_Txt;
    End;
    select Generate_Tree_Fun(sys_connect_by_path(e.empno,','),',',level,'emp','mgr','empno') || e.ename
    from emp e
    start with mgr is null
    connect by e.mgr=prior e.empno
    enjoy it

    Arash wrote:
    hi,
    i tried to write a function that generate a actual tree select and i think this is very helpful
    We are so proud of you!

  • Purchase Order : function to determine the purchase order number

    Hi,
    I'm working on data migration to create purchase order. I must use the internally range number. Does anybody knows which function must i use to create the purchase order number before calling the bapi bapi_po_create ?
    with regards
    Jean-Luc Ledoux

    Hi,
    The BAPI is designed to generate the next free number that is defined by the company and allocate it in series.
    This is taken care internally.
    explain the scenario like as from what data ur creating the PO and wht exactly do u mean by having ur own internal number range.....
    No body will have 2 number ranges to define PO....
    if ur uploading data from legacy system then u can have 2 fields called as old purchase order number and purchase order number in new erp system.
    if this is ur scenario ask how the functional mapping is done in the company to accomadate legacy purchase order numbers.
    Regards,
    Jayaram Maganti

  • Incrementing Number on Adobe Designer 7.0 Form

    Hello -
    I am wanting to create a PO form where an incrementing number gets applied to a field each time a user opens the form. The user is creating a purchase order, and instead of using preprinted-paper forms he/she wants to use an Adobe form. I have created the entire form in Adobe Designer 7.0, however, I cannot seem to get an incrementing number to be calulated/populated into a field. The form then gets mailed to others, however, when they open the form the number should not change because it is already populated. It should only populate (and increment the counter) when the field is blank (i.e. a new purchase order is being created).
    I got something like this to work in Acrobat Pro 6.0 using a "global tracking number"....
    Please help.
    Thank you.
    Rick

    I'm not familiar with the "global tracking number" functionality you are referring to (I couldn't find any such beast in Acrobat 7 Pro), but it would seem to me that what you need is some sort of database connectivity. The PO number field would be the index on a PO table. If the customer is intent on keeping the PO numbers unique, would they not be tracking these is some sort of database repository?

  • Very Urgent : how to generate continious sequential No. (se 38 + smartform)

    Dear All,
                 Could you plz tell me how to generate continious sequential  No.. Atually i have to print TC No. on the form, but it's not available in the SAP. TC No. should be in sequence i.e for first time say it 's 10, at the second time it should be 11, third time it should be 12 and so on.
    Plz tell me ASAP. It's very urgent.
    Looking forward to your earliest response.
    thanks in Advance.
    Gulrez Alam

    You can create a number range(SNRO) to get the continuous numbers.You can use the FM 'NUMBER_GET_NEXT' to get the next number from number range.

  • Incrementing Number in Header

    Is there a way to add an incrementing number (like a PO number) in the header section of the form?  The number would be automatically generated when a form is requested and people filling out the form would not be able to edit the number?  

    We do not currently support this feature but you can vote for it here : http://forums.adobe.com/ideas/1047

  • How to add increment number to file name properly

    Hi, all:
    I have a simple question. As shown in the attached code, I need to add an increment number to file name and save the file. The problem is, if you run the code and put something like ".dat" in the extension name, you will see a blank space between the number and the dot in the file name. What should I do to eliminate the blank space? I can't specify the format of the file name since the file name would vary from case to case.
    Thanks a lot for any suggestion and correction in advance.
    Attachments:
    temp.vi ‏11 KB

    Hi,
    Please take a look to the attached VI. I hope that I understood what you really want.
    I've replaced the Vi: Format into string with Number to Decimal String plus a Concatenate String. 
    Now, this is working for me, I don't get a space anymore.  If something is wrong, please let me know.
    Regards,
    Paul
    Attachments:
    temp_back.vi ‏11 KB

  • Use SQL function to get the original order number using the invoice number

    Hi All,
    wondering is someone can help me with this challenge I am having?  Often I need to return the original order numbers that created the resulting invoce.  This is a relatively simple seriese of joins in a query but I am wanting to simplify it using a SQL function that can be referenced each time easily from with in the SELECT statement.  the code i currently have is:
    Use SQL function to get the original order number using the invoice number
    CREATE FUNCTION dbo.fnOrdersThatMakeInvoice(@InvNum int)
    RETURNS nvarchar(200)
    AS
    BEGIN
    DECLARE @OrderList nvarchar(200)
    SET @OrderList = ''
    SELECT @OrderList = @OrderList + (cast(T6.DocNum AS nvarchar(10)) + ' ')
    FROM  OINV AS T1 INNER JOIN
          INV1 AS T2 ON T1.DocEntry = T2.DocEntry INNER JOIN
          DLN1 AS T4 ON T2.BaseEntry = T4.DocEntry AND T2.BaseLine = T4.LineNum INNER JOIN
          RDR1 AS T5 ON T4.BaseEntry = T5.DocEntry AND T4.BaseLine = T5.LineNum INNER JOIN
          ORDR AS T6 ON T5.DocEntry = T6.DocEntry
    WHERE T1.DocNum = @InvNum
    RETURN @OrderList 
    END
    it is run by the following query:
    Select T1.DocNum, dbo.fnOrdersThatMakeInvoice(T1.DocNum)
    From OINV T1
    Where T1.DocNum = 'your invoice number here'
    The issue is that this returns the order number for all of the lines in the invoice.  Only want to see the summary of the order numbers.  ie if 3 orders were used to make a 20 line inovice I only want to see the 3 order numbers retuned in the field.
    If this was a simple reporting SELECT query I would use SELECT DISTINCT.  But I can't do that.
    Any ideas?
    Thanks,
    Mike

    Thanks Gordon,
    I am trying to get away from the massive table access list everytime I write a query where I need to access the original order number of the invoice.  However, I have managed to solve my own problem with a GROUP BY statement!
    Others may be interested so, the code is this:
    CREATE FUNCTION dbo.fnOrdersThatMakeInvoice(@InvNum int)
    RETURNS nvarchar(200)
    AS
    BEGIN
    DECLARE @OrderList nvarchar(200)
    SET @OrderList = ''
    SELECT @OrderList = @OrderList + (cast(T6.DocNum AS nvarchar(10)) + ' ')
    FROM  OINV AS T1 INNER JOIN
          INV1 AS T2 ON T1.DocEntry = T2.DocEntry INNER JOIN
          DLN1 AS T4 ON T2.BaseEntry = T4.DocEntry AND T2.BaseLine = T4.LineNum INNER JOIN
          RDR1 AS T5 ON T4.BaseEntry = T5.DocEntry AND T4.BaseLine = T5.LineNum INNER JOIN
          ORDR AS T6 ON T5.DocEntry = T6.DocEntry
    WHERE T1.DocNum = @InvNum
    GROUP BY T6.DocNum
    RETURN @OrderList 
    END
    and to call it use this:
    Select T1.DocNum, dbo.fnOrdersThatMakeInvoice(T1.DocNum)
    From OINV T1
    Where T1.DocNum = 'your invoice number'

  • ITunes appends incrementing number to file name

    Hi,
    I noticed that with some of my mp3 files iTunes 6 keeps on creating copies of them, appends an incrementing number to the file name and adds the new file to my iTunes library, which means I end up with multiple copies of the same song in my library.
    So if I have got a file called "03 All I Need.mp3" iTunes will create
    03 All I Need 1.mp3
    03 All I Need 2.mp3
    03 All I Need 3.mp3
    etc...
    Perhaps it could be related to the fact that I have got two computers accessing the same mp3 folder via a network?
    I really can't explain to myself why iTunes would be doing this, but it is terribly annoying, as I end up with the same song over and over again in my iTunes library.
    Does anybody know how I can prevent iTunes from doing that?
    Thanks heaps!

    Hi,
    Please take a look to the attached VI. I hope that I understood what you really want.
    I've replaced the Vi: Format into string with Number to Decimal String plus a Concatenate String. 
    Now, this is working for me, I don't get a space anymore.  If something is wrong, please let me know.
    Regards,
    Paul
    Attachments:
    temp_back.vi ‏11 KB

  • Generate a sequence of number

    Hi i am using
    select rownum rn from user_objects where rownum <= 20;
    to generate a sequence of number....
    is there any other better way...???

    try
    select level
    from dual
    connect by level <= 20;
    illustrated at -
    http://nimishgarg.blogspot.com/2010/01/oracle-sql-get-all-month-names-jan-to.html

  • ABAP Function Module generator

    Hi,
    I wonder if there is a ABAP Function Module generator for inbound and outbound adapter data. Can I generate a function module in XI to trigger a adapter.
    regards,
    Richard

    Hi,
    There is no tool to "generate" RFCs.  You will have to use SE37 to create the RFCs as always.
    For outbound RFC, you will need only to define the interfaces, e.g. IMPORT, EXPORT, TABLES.  No ABAP code is required.  Then, just populate the parameters and send to XI.
    For inbound RFC, you can develop the RFC to do whatever it needs to do in ABAP.
    In the IR of XI, you can import the RFC's meta data.
    Regards,
    Bill

  • Using user defined text functions to generate strings on button.

    I am new to java programming and am facing a problem.. It would be great if you could help me resolving it..
    The problem is:
    Is it possible to use user defined functions to generate the string on a button(button name)?
    If it is possible please educate me on it..
    Thanks..

    Yes its possible. What you ask is so vague that it can be interpreted in so many ways there are plenty correct answers
    public void userDefinedFunction(String aString)
    yourButton.setText(aString);
    }

  • Generating non-sequential primary keys

    Is there a simple way to generate non-sequential primary keys? Say for instance I want to generate a user ID. I'd like all of the IDs to be unique, but not strictly sequential.

    >
    Hi Crottyan,
    Is there a simple way to generate non-sequential primary keys? Say
    for instance I want to generate a user ID. I'd like all of the IDs to be
    unique, but not strictly sequential.Normally this is done to eliminate contentation for index inserts - why do
    you want to do it?
    Be careful with GUID - it can be [url http://en.wikipedia.org/wiki/Globally_unique_identifier#Sequential_algorithms]sequential.
    It's a pity you didn't tell us your version of Oracle :( - you should always do this.
    10 XE has a sequential algorithm. 11 XE doesn't.
    HTH,
    [Edit] Just saw your post about inferring insert order - fair enough. Will be able to tell you about 11 EE next
    week ;)
    Paul...
    Edited by: Paulie on 18-Jul-2012 18:03

Maybe you are looking for

  • Image not displaying properly in pdf report

    Hi, I have imported a jpg image on the layout using Reports builder 6i folowing File-Import-Image. Its just a static image. Once i move this report to the server in binary mode and open it from apps(Quote pdf report), the report pdf has the image dis

  • Workflow not getting triggered from webdynpro event

    Hello gurus, I have a requirement in which if an employee changes his own information on the ESS portal, then an approval should be done by HR. For this i created a custom webdynpro application in which i fetch the employee data and check it with old

  • IStore preferred address

    Hi there, Oracle® iStore Implementation and Administration Guide for 11.5.10 says as following on page 461 : Preferred addresses -- Users can flag one billing and one shipping address as preferred. Oracle iStore then uses the preferred addresses when

  • Macbook Pro Retina 13 inch Mid 2014 Bad battery.

    Hello, 2 months after i got my macbook pro retina, my battery started dropping. For example, at 100%, it says 4 hours remaining, it used to have 8. I tried restarting PRAM, but nothing changed. It started to happen ever since i updated to Yosemite i

  • Access server email locally

    Hi, I have what might be a strange question. I have an older server that is running ML and I am not longer using it as an eail server.  It is connected to my local network and I can access the files but how can I access/read the emails that are store