Numbering groups of records

I need to create a SQL statement that will create a unique identifer for groups of records returned by a join. This number will be used to differentiate individual records in groups where all columns are the same except for one. I can use a function to do this but I am trying to avoid temporary tables. This SQL statement will be used to populate a view.
Here is basically what it should look like:
Col1 Col2 Col3 Col4 OBS
A A A 5 1
A A A 4 2
B A A 3 1
B A B 2 1
B A B 3 2
B A B 7 3
A B A 6 1
(OBS is the number I need to generate, col4 is the one column whose value can vary in the group. Col 1-4 come from a join)
Any ideas or suggestions would be appreciated.
Thanks,
David
null

Hi David-
If OBS is used only to differentiate and possibly for sorting, then it doesn't have to start over at 1 for each new group. You can create a sequence named OBS_seq and reference it for the value of OBS. Each row in the entire resultset gets a unique value for OBS and by deduction, each row in a group has a unique value of OBS.
If you implement this technique, you will probably wish to define a time when the sequence can be dropped and re-created to occasionally start over at 1.
David

Similar Messages

  • Assigning sequential value that resets to a group of records

    Hello all, and happy FRIDAY!!!
    I'm doing some data conversion for a new system and I'm having trouble coming up with a query.
    Running Oracle 10.2
    Sample Data:
    create table test (GRP number, PART varchar2(20), SEQ number);
    insert into test values(9000, 'lskdjf', null);
    insert into test values(9000, 'alsdk', null);
    insert into test values(9000, '492kjsfjsldk', null);
    insert into test values(9000, 'lkjasdf0982j', null);
    insert into test values(9001, 'likfjajsd', null);
    insert into test values(9001, '234-092838', null);
    insert into test values(8934, '000-192893aj', null);
    insert into test values(8934, 'anotherpart', null);
    insert into test values(8934, 'jjjj0-aa-2001', null);
    insert into test values(8934, 'encifudy', null);
    insert into test values(8934, 'asfdjslkjdfklsj', null);
    insert into test values(8934, 'lksjdflj', null);
    insert into test values(4736, 'l;ask---jdflasj', null);
    commit;
    Select * from test;Doing the select * at this point will give you the null data elements for the third column.
    Problem:
    I'm trying to run a query that spits out a sequence for each "group" of records. If there were 5 records in a group, I would want the sequence to start at 1 and go to 5... maybe ordered by part. (not quite sure the ordering, but need to do babysteps here) :P
    Here is an example of the data output I'd want from the above example:
    GRP     PART               SEQ
    9000     lskdjf               1
    9000     alsdk               2
    9000     492kjsfjsldk          3
    9000     lkjasdf0982j          4
    9001     likfjajsd          1
    9001     234-092838          2
    8934     000-192893aj          1
    8934     anotherpart          2
    8934     jjjj0-aa-2001          3
    8934     encifudy          4
    8934     asfdjslkjdfklsj          5
    8934     lksjdflj          6
    4736     l;ask---jdflasj          1In that result, SEQ is not applied to the record in part order, but ultimately that might be something that I'd like to do. So far just working toward wanting to get those numbers in there. I have read using rankover(), but that is still a bit confusing to me on how that works. I'll go research it more as I suspect that is probably what I'm going to have to use.
    Ultimately this is part of a cursor that is being fed through a bulk collect. I might be able to populate that SEQ in the PL/SQL, but I figured it'd be easier to just get them in place at the query level so they're already part of the collection, instead of having to build the logic to create that numbering system prior to insertion to a staging table.
    Anyhoo... any help would be greatly appreciated! If I left anything out or am un-clear in anyway, please let me know! Thank you!
    Edited by: dvsoukup on Jul 27, 2012 4:25 PM

    Hi,
    dvsoukup wrote:
    Running Oracle 10.2
    Sample Data:
    create table test (GRP number, PART varchar2(20), SEQ number);
    insert into test values(9000, 'lskdjf', null); ...
    Thanks for posting your version, and the CREATE TABLE and INSERT statemen ts; that's very helpful!
    >
    Doing the select * at this point will give you the null data elements for the third column.
    Problem:
    I'm trying to run a query that spits out a sequence for each "group" of records. If there were 5 records in a group, I would want the sequence to start at 1 and go to 5... maybe ordered by part. (not quite sure the ordering, but need to do babysteps here) :P
    I have read using rankover(), but that is still a bit confusing to me on how that works. You've got that right! Analytic functions are very strange looking and confusing at first. After a while, they don't seem so strange, and then they even get less confusing.
    One thing to remember: "PARTITION BY x" (this clause is always optional) means that each value of x is a world unto itself. It's as if a separate query is being done for each value of x, and when they're all finished, the results are UNIONed together. In this case, you want to PARTITION BY grp.
    I'll go research it more as I suspect that is probably what I'm going to have to use.You're very close; only, as the first reply said, it's not RANK that you want, but its close relative ROW_NUMBER. The difference between the two is how they handle ties. If you have duplicate data, RANK assigns duplicate numbers. I assume you want unique numbers, even if you have identical parts in the same grp.
    Ultimately this is part of a cursor that is being fed through a bulk collect. I might be able to populate that SEQ in the PL/SQL, but I figured it'd be easier to just get them in place at the query level so they're already part of the collection, instead of having to build the logic to create that numbering system prior to insertion to a staging table.It's easy enough to assign the numbers when you build the table.
    If the situation is what you posted, that is, your table already exists, but the seq number isn't populated yet, then you can do this:
    MERGE INTO     test     dst
    USING   (
             SELECT  grp
             ,         part
             ,         ROW_NUMBER () OVER ( PARTITION BY  grp
                                                    ORDER BY      part
                               )       AS seq
             FROM    test
         )          src
    ON     (     src.grp          = dst.grp
         AND     src.part     = dst.part
    WHEN MATCHED THEN UPDATE
    SET     dst.seq   = src.seq
    ;The MERGE statement above assumes that the combination (grp, part) is unique.
    After you run it, this query:
    SELECT       *
    FROM       test
    ORDER BY  grp
    ,            part
    ;will produce these results:
    `      GRP PART                        SEQ
          4736 l;ask---jdflasj               1
          8934 000-192893aj                  1
          8934 anotherpart                   2
          8934 asfdjslkjdfklsj               3
          8934 encifudy                      4
          8934 jjjj0-aa-2001                 5
          8934 lksjdflj                      6
          9000 492kjsfjsldk                  1
          9000 alsdk                         2
          9000 lkjasdf0982j                  3
          9000 lskdjf                        4
          9001 234-092838                    1
          9001 likfjajsd                     2The USING clause above is almost the same as the query posted in the last message, but, in the analytic clause, instead of "ORDER BY grp, part" it only says "ORDER BY part". It never makes any sense to PARTITION BY and ORDER BY the same column in the same function. Why? Discuss.

  • How to Select each check box values in a group of records

    Hi,
    I have a requirement in forms 10g. In that form there are 10 records are displaying each record has one check box is there if i click the check box on record number one and record number three and do some changes in the text field(adjustment field is number data type) then finally I want to see the total on one field called total amount.
    In this my question is how to select particular records in a group of records? and finally these selected records are inserted into one table.
    Because I am not able to fetch these records at a time.
    Is there any Array to define a record group to fetch each of them individually for example Rec[1],Rec[2]...like that if yes please suggest me the steps how to do this.
    Thanks in advance
    Prasanna
    Edited by: user10315107 on Dec 17, 2008 11:44 PM

    I'm sorry, but i didn't get your requirement in detail.
    Do you want to do the summing of the selected records in forms ? Or do you just want to know which records are selected and then process them?
    If you want to process the selected records in sql you could use an object-type to store the list of id's (of whatever primary key you use), loop over the block to fill it, and then afterwards process them.
    For this approach, first create an object-type in the database:
    CREATE OR REPLACE TYPE ID_LIST AS TABLE OF NUMBER;
    /Then, in forms you could do something like this to fill a list of id's:
    DECLARE
      lIds ID_LIST:=ID_LIST();
    BEGIN
      GO_BLOCK('MYBLOCK');
      FIRST_RECORD;
      LOOP
        EXIT WHEN :SYSTEM.RECORD_STATUS='NEW';
        IF :BLOCK.CHECKBOXITEM="CHECKEDVALUE" THEN
          lIds.EXTEND(1);
          lIds(lIds.COUNT):=:BLOCK.PRIMARYKEYITEM;
        END IF;
        EXIT WHEN :SYSTEM.LAST_RECORD='TRUE';
        NEXT_RECORD;
      END LOOP;
      -- Now you can use the object-list in SQL like :
      INSERT INTO MYNEWTABLE (cols..)
      SELECT (cols..)
        FROM MYOLDTABLE
       WHERE ID IN (SELECT COLUMN_VALUE FROM TABLE(lIds));
    END;Edited by: Andreas Weiden on 18.12.2008 18:17

  • Error while deleting Cash Journal (Numbering group 1001 can be used only for one CoCd and cash journal number)

    Hi,
        While i am try to delete Cash Journal system throws me error (Message No.:FCJ065: Numbering group 1001 can be used only for one Co. Code  and Cash Journal). Kindly provide me solution on that.
    Regards,
    Tarak

    Hi Tarak,
    You can use the config nodes in SPRO to set up number ranges for your cash journal documents.
    IMG Path> FI Global Settings>Bank Accounting>Business transactions>Cash Journal
    Define Number Range Intervals for cash Journal documents
    Define Numbering Groups
    Define Number Ranges for numbering groups
    After defining the number group here, you assign that in "Set up Cash Journal" Check if there is any clash anywhere in your configuration for different cash Journals.
    Regards,
    Kavita

  • Withholding tax -number could not be determined for numbering group ID0017

    While positing the FB60 transaction with withholding tax for country Indonesia, the below error is coming:
    A number could not be determined for numbering group ID0017
    Message no. 7Q630
    "The system could not determine a certificate number for the numbering
    group.
    Withholding tax types exist that are relevant to numbering. The system
    cannot determine a certificate number because the Customizing settings
    are incomplete.
    Sysem Response
    Payment cannot be made.
    Procedure
    Check the number ranges in the numbering group."
    Already seen the wothholding tax number range and it is coorec. Please advise how to resolve.

    Dear,
    Please check whether you have assigned the number range to the number group in the below setting
    Spro
    Financial Account Global Setting> Withholding Tax>  Extended Withholding Tax> Posting> India> Remittance Challan>     Assign Number Range to Number Groups

  • 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;

  • 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;

  • Problem with showing a group of records in dynamic page (PLS-00428  error)

    Hi
    I have problems with viewing a group of records from db. I've created Page, which "Page Type" attribute is "PL/SQL". In "PL/SQL Code" field, I've written very simple SQL query: "SELECT * FROM PORTAL_DEMO.EMP ;"
    I'd like to generate HTML table with it's result. Unfortunately, every time I get the same Exception:
    Error 30584: DBMS_SQL has raised an unhandled exception. ORA-06550: line 1, column 7: PLS-00428: an INTO clause is expected in this SELECT statement
    Is it necessary to write a COURSOR (and use HTP function) to realize this functionality? I succeed, trying this way - but it is very uncomfortable ( a lot of code ).
    What I have done in wrong way?
    Please help me.
    Best regards
    Mario

    How do I handle this?

  • Numbering Group G1 can be used only for one CoCd and cash journal number

    Hello,
    I was trying to create a cash journal when I got the error message:
    "Numbering Group G1 can be used only for one CoCd and cash journal number".
    Does anyone have an idea what could be wrong?
    Thank you,
    Bola

    Hi,
    Please check whether more than one number range is maintained in  transaction code FBCJC1. If yes, please keep only one as Cash Journal has only one number range. This is the internal number range for Cash Journal.
    If you want different number range for accounting documents then create new document types and assign number ranges to it.
    Regards,
    Tejas

  • Urgent: Oracle Reports : Blank Page Should print after each Group of record

    Hi all,
    I have a different kind of requirement, in my report I have 1 block and I want to generate a blank page after every group of records. This is because the printer is printing both side of the paper and we are giving multiple group at same time.
    Cannot touch the printer setup since it is a common one. Anybody can help me in this for a fast and simple way ? "Maximum records per page" property for the repeating frame give the next group to a fresh page but there will not be a blank page in between.
    Please help ..
    Thanks,
    Ani.

    Thank you for your attention.
    Empty frame with page break is not helping me since it will not hide the rest of the items which are outside the repeating frame. Like Report caption, Group Name, Page footer etc...
    If I am using the empty frame, is there any way to hide all other items ?
    Thanks,
    Anish

  • Grouping few records.. Please help me....

    Hi all,,
    I have the following doubt.
    I have a field by name area and it is filled with many values as shown below
    I would like to group them as Locations.
    For example
    Area
    abc1
    abc2
    abc3
    abc4
    abc5
    abc6
    I would like to group first three records and get the result as Location
    That means when i run this query i want to name it as Location
    Thanks and Regards
    Nick

    This is even more confusing :
    Grouping few records.. Please help me....

  • SQL to group the records and apply logic to pick one record from each group

    Hi Friends,
    I am looking for a query to group the records on certain columns in a table and then from each group I want to pick only one record based on certain rules.
    May be having data laid out will make my point more clear to you. Here you go :
    CREATE TABLE AD_LIST
      FILE_NAME             VARCHAR2(50 BYTE),
      ACTIVITY_START        DATE,
      ACTIVITY_END          DATE,
      DIVISION              VARCHAR2(50 BYTE),
      ITEM_CODE             VARCHAR2(50 BYTE),
      MULT                  NUMBER,
      RETAIL                NUMBER,
      AD_PAGE               VARCHAR2(1 BYTE),
      FORECAST              NUMBER,
      MEMO                  VARCHAR2(50 BYTE)
    INSERT INTO AD_LIST VALUES ('FILE_1','01-APR-2010','15-APR-2010','B',1111,5,10,'A',10,'This must be in my result');
    INSERT INTO AD_LIST VALUES ('FILE_1','01-APR-2010','15-APR-2010','B',1111,1,1,'B',15,'Must not be in my result');
    INSERT INTO AD_LIST VALUES ('FILE_1','01-APR-2010','15-APR-2010','B',1111,6,15,'C',11,'Must not be in my result');
    INSERT INTO AD_LIST VALUES ('FILE_1','16-APR-2010','30-APR-2010','N',1111,4,20,'D',40,'Must not be in my result');
    INSERT INTO AD_LIST VALUES ('FILE_1','16-APR-2010','30-APR-2010','N',1111,5,15,'E',30,'Must not be in my result');
    INSERT INTO AD_LIST VALUES ('FILE_1','16-APR-2010','30-APR-2010','N',1111,1,2,'F',20,'This must be in my result');
    CREATE TABLE PAGE_RANK
      AD_PAGE VARCHAR2(1 BYTE),
      RANK NUMBER
    INSERT INTO PAGE_RANK VALUES ('A',1);
    INSERT INTO PAGE_RANK VALUES ('B',2);
    INSERT INTO PAGE_RANK VALUES ('C',3);
    INSERT INTO PAGE_RANK VALUES ('D',4);
    INSERT INTO PAGE_RANK VALUES ('E',5);
    INSERT INTO PAGE_RANK VALUES ('F',6);
    COMMIT;
    SELECT * FROM AD_LIST
    FILE     ACTIVITY     ACTIVITY          ITEM               AD
    NAME     START          END          DIV     CODE     MULT     RETAIL     PAGE     FORECAST     MEMO
    FILE_1     4/1/2010     4/15/2010     B     1111     5     10     A     10     This must be in my result
    FILE_1     4/1/2010     4/15/2010     B     1111     1     1     B     15     Must not be in my result
    FILE_1     4/1/2010     4/15/2010     B     1111     6     15     C     11     Must not be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     4     20     D     40     Must not be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     5     15     E     30     Must not be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     1     2     F     20     This must be in my resultNow, from the table AD_LIST I want to group the records based on FILE_NAME, ACTIVITY_START, ACTIVITY_END, DIVISION, ITEM_CODE.
    So in my example here we have 2 set of records grouped based on the columns specified.
    Also we have one more table, PAGE_RANK, which has a rank corresponding to each ad_page number. Here 1 is higher rank than 2. Hence ad page 'A' takes priority over 'B'. Similarly for all other ad pages.
    Now, we need to pick one ad from each group of ads by determining the highest ranked ad page within the group and the value for mult and retail must be replaced with the value that has min(retail/mult). So, using the above data we will have the one having ad page = 'A' and ad page = 'D' as the final results since they have highest ad page rank in their group.
    The value for mult and retail values for ad_page 'A' = min (10/5 , 1/1, 15/6) = 1,1(mult,retail).
    The value for mult and retail values for ad_page 'D' = min (20/4 , 15/5, 2/1) = 1,2(mult,retail).
    Finally I have this query below
    SELECT a.file_name,
           a.activity_start,
           a.activity_end,
           a.division,
           a.item_code,
           FIRST_VALUE (a.mult) OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY (a.retail /
                                                                                                                                    a.mult))
                                                                                                        mult,
           FIRST_VALUE (a.retail) OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY (a.retail /
                                                                                                                                      a.mult))
                                                                                                      retail,
           FIRST_VALUE (a.ad_page) OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY (b.RANK))
                                                                                                     ad_page,
           a.forecast,
           a.memo                                                                                                
      FROM ad_list a, page_rank b
    WHERE a.ad_page = b.ad_pageThis query is giving me all the records but with the values what I wanted in Ad_Page, Mult and Retail columns.
    How can I pick only one from each group.
    I am getting this FILE     ACTIVITY     ACTIVITY          ITEM               AD
    NAME     START          END          DIV     CODE     MULT     RETAIL     PAGE     FORECAST     MEMO
    FILE_1     4/1/2010     4/15/2010     B     1111     1     1     A     15     Must not be in my result
    FILE_1     4/1/2010     4/15/2010     B     1111     1     1     A     10     This must be in my result
    FILE_1     4/1/2010     4/15/2010     B     1111     1     1     A     11     Must not be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     1     2     D     20     This must be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     1     2     D     30     Must not be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     1     2     D     40     Must not be in my resultBut I want this FILE     ACTIVITY     ACTIVITY          ITEM               AD
    NAME     START          END          DIV     CODE     MULT     RETAIL     PAGE     FORECAST     MEMO
    FILE_1     4/1/2010     4/15/2010     B     1111     1     1     A     10     This must be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     1     2     D     20     This must be in my resultI have to run this query for thousands of such group combination.
    Hope some one can throw some light on this query.
    Thanks in advance,
    Raj.

    Frank,
    You are marvelous.
    That is what I was expecting, but basically I want to display the row with highest page rank which is 'A' and 'D' in this case.
    So I have changed my query as below using yours :
    WITH mainq AS
         (SELECT a.file_name,
                 a.activity_start,
                 a.activity_end,
                 a.division,
                 a.item_code,
                 FIRST_VALUE (a.mult) OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY (a.retail /
                                                                                                                                          a.mult))
                                                                                                        mult,
                 FIRST_VALUE (a.retail) OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY (a.retail /
                                                                                                                                            a.mult))
                                                                                                      retail,
                 --FIRST_VALUE (a.ad_page) OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY (b.RANK))
                 a.ad_page,
                 a.forecast,
                 a.memo,
                 ROW_NUMBER () OVER (PARTITION BY a.file_name, a.activity_start, a.activity_end, a.division, a.item_code ORDER BY b.RANK)
                                                                                                 AS r_num
            FROM ad_list a, page_rank b
           WHERE a.ad_page = b.ad_page)
    SELECT *
      FROM mainq a
    WHERE r_num = 1
    FILE     ACTIVITY     ACTIVITY          ITEM               AD
    NAME     START          END          DIV     CODE     MULT     RETAIL     PAGE     FORECAST     MEMO
    FILE_1     4/1/2010     4/15/2010     B     1111     1     1     A     10     This must be in my result
    FILE_1     4/16/2010     4/30/2010     N     1111     1     2     D     40     Must not be in my resultMy apologies that I gave you wrong forecast and memo values in my earlier post.
    But that is what I wanted and your input greatly helped me to save lot of time by using in one single query.
    Earlier I was using cursor to do that and it was not doing any good performance wise.
    Thanks to every body for your time and your efforts.
    I appreciate it.
    Have fun.
    ~Raj

  • Assigning sequential numbers for every lines within a group of records

    The scenario is:
    This set of records with group number, lets say 100(group number) contains 7 lines/records. How to assign line numbers (sequential) for each line within these groups on the fly during the mapping process before inserting these set of rows in the target. I know it is easy to achieve in a procedure, but not sure how to do this in the mapping.
    please advice.
    Thanks,
    Prabha

    Use Rank function
    SQL> select empno,ename,deptno,(rank() over (partition by deptno order by empno)) seqno from emp;
    EMPNO ENAME DEPTNO SEQNO
    7782 CLARK 10 1
    7839 KING 10 2
    7934 MILLER 10 3
    7369 SMITH1 20 1
    7566 JONES 20 2
    7788 SCOTT 20 3
    7876 ADAMS 20 4
    7902 FORD 20 5
    7499 ALLEN 30 1
    7521 WARD 30 2
    7654 MARTIN 30 3
    7698 BLAKE 30 4
    7844 TURNER 30 5
    7900 JAMES 30 6
    1111 Test 40 1
    1222 test 1
    1333 2
    17 rows selected

  • Grouping the Records Into One Single Record

    Suggest me any function in sql or script which gives me following result.
    I have a table in which there are few columns and the data looks like below. I am only concerned about 3 columns
    C1 C2 C3
    A B Parent
    B A Child
    My requirement is to club and make it as
    C1 C2 C3 C4
    A B Parent Child
    I need to group in .. If we look into the first 2 records .. c1 of first record is equal to c2 of second record..

    Hi,
    Here's one way to do it:
    SELECT     p.c1
    ,     p.c2
    ,     p.c3
    ,     c.c3     AS c4
    FROM     table_x  p
    JOIN     table_x      c  ON   p.c2  = c.c1
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Point out where the query above is producing the wrong results and explain, using specific examples, how you get those results from the given data in those places.
    Always say which version of Oracle you're using.
    See the forum FAQ {message:id=9360002}

  • Member of Target Group: Contact record updated

    We are creating a target group for a Campaign and several of the contacts had to be updated (changed Companies "Account" for instance) and the new data for that contact is not reflected on Members list, shows old data (old Company "Account").
    Without removing the member and re-adding, are we missing something or once they are added it holds the old data?
    We can find this down the road to be cumbersome when copying target groups and/or when using the same target group for other campaigns
    if a contact record is updated and the information is not matching to the member data.
    Any insight appreciated!

    Hi Gina,
    once the members are added to a group and after if you do any changes on the master data these changes wont reflect in the group already created, you should remove and save the group after again add back this time the new changed data will be updated.
    hope this clarify your issue.
    Regards,
    Suresh

Maybe you are looking for