3 table join finding matching audit records

hi,
I have the following 3 tables ( simplified for sake of example)
TOYS ( rec_no, toy_id)
CHANGE (rec_no, change_id, status, aff_toy_id)
A_CHANGE (rec_no, change_id, aff_toy_id, status, operation, log_date, log_time)
I want to find all the TOYS where a change status was made from PENDING to COMPLETE. The audit table A_CHANGE has an operation field which has 2 possible values. PRE_UPDATE and POST_UPDATE.
the PRE_UPDATE record would have a status of PENDING and the POST_UPDATE record would have a status of COMPLETED.
However for any record there could have been many status changes, thus i need to pair the PRE and POST UPDATE records correctly. Currently i have used the following SQL which assumes that the date and time for the PRE and POST would be exactlyl the same.
create or replace view test as
select a.aff_toy_id
from a_change a, a_change b, change c, toys d
where c.aff_toy_id=d.toy_id
and c.status='COMPLETE'
and a.change_id=c.change_id
and a.change_id=b.change_id
and a.operation='PRE_UPDATE'
and b.operation='POST_UPDATE'
and a.log_date=b.log_date
and a.log_time=b.log_time
and a.status='PENDING'
and b.status='COMPLETE';
this is not reliable as the PRE and POST may be a minute a part. What i need to do is for any given PRE_UPDATE find the associated POST_UPDATE based on the next sequential rec_no and primary key change_id.
How do i do this? Maybe i need an external function or procedure? Or is this possible within a single select statement?
big thanks to anyone who can help me here.
cheers
D
Message was edited by:
user472959

I have used lead analytical function. Analytical functions were introduced by oracle in realease 8.1.6. There are over 30 of them. You can get more information about them on the net. Lead function retrieves the value of a column in the next record. Next record is determined by the ORDER BY clause of the function. To group the result by a column or set of columns you use PARTITION BY clause.
Visit http://asktom.oracle.com/pls/ask/f?p=4950:1:
Anwar

Similar Messages

  • Table Join brings in multiple records

    Hi guys,
    I have created a table join for 2 HR master tables to get a view.
    When i output the view however it brings in multiple records for each employee.
    I have set PERNR as the Key field to join the tables but instead of giving me one instance of employee it picks it up 2 records....and in some cases many more, is their a fix for this?
    Thanks

    Hi,
    Try using a combined primary key;
    PERNR, BEGDA and ENDDA
    There are more primary keys to be combined but try with this 3.
    ~BiSu

  • One to Many table join -- concat field per record grouped by id

    Post Author: wm5
    CA Forum: Formula
    Hello,
    I am using Crystal Reports XI and have two tables that have a one to many relationship and are joined by an JobID (number).
    Below is a sample with relative fields for each table.
    job_table: JobID (number), Manager (text), Status (text)
    jobaudit_table : JobAuditID (number), JobID (Number), FormID (Number)
    There is a one to many relationship with jobaudit_table having multiple records for each JobID in job_table.
    I have created a Group Header using the job_table.JobID and suppressed the detail section.
    In the group header for each JobID I display the JobID, Manager, Status. I also use the below formula to determine if any records in the jobaudit_table has a record where FormID = 90. If so, I display "Yes". If not, "No".
    So my report currently looks like.
    JobID     Manager     Status     Audit Performed
    1           Manager 1  Closed           N
    2          Manager 2   Closed           Y
    Here are the formula's I use to determine if any records in jobaudit_table contains a record where FormID = 90.
    @ja90exists
    if {jobaudit_table.FormID} = 90 then    1else    0;
    if sum({@ja90exists},{job_table.JobID}) = 0 then    "No"else    "Yes";
    Everything so far works fine. What I would like to do now is add a hyperlink to a script to view the job audit when in the above report the "Audit Performed" column is "Yes"
    So Report is now:
    JobID     Manager     Status     Audit Performed
    1           Manager 1  Closed           N
    2          Manager 2   Closed           Y (hyperlink to view audit)
    I cannot figure out how to gather the valid JobAuditIDs where FormID = 90 grouped by JobID to be used in the Group Heading section of the report.
    Also, it is unlikely, but possible that more than one job_audit record exists with FormID = 90 per JobID. So, my hyperlink could look like http://mysite.com/viewjobaudit.aspx?jobid=[jobaudit_table.JobAuditID],[jobaudit_table.JobAuditID] .
    Thanks for any help. And if this post is not clear let me know and I will clarify.
    wm5

    Post Author: bettername
    CA Forum: Formula
    Although I can't think of a way to get multiple hyperlinks, this should be a start.  It (should) hyperlink to the last job/audit in the group that formID of 90.  Oh, I assumed that the hyperlink should have been  xxxx...jobID,jobAuditID! 
    I think there may be a way of getting hyperlinks to every "90" record, but that will involve a subreport, so lets try this first...
    1 - everything from your group header to the group footer...
    2 -  add a formula into the group header that says:
    whileprintingrecords;
    stringvar jobauditID="";
    stringvar jobID:=";
    3 - Then add a formula to the details section:
    whileprintingrecords;
    stringvar jobauditID;
    stringvar jobID;
    if {jobaudit_table.FormID} = 90
    then (jobID:=totext({job_table.job_id},0,""); jobauditID:=totext({jobaudit_table.jobaudit_id},0,"")
    4 - Finally, on your "Audit Performed" formula, have a conditional hyperlink that says:
    whileprintingrecords;
    stringvar jobauditID;
    stringvar jobID;
    if {@audit performed} = "Y" then http://mysite.com/viewjobaudit.aspx?jobid=jobID","+jobauditID

  • To find matching records across two tables

    Hi folks,
    I have a question regarding modifying the query. I am trying to compare the data between two tables, there are matching records in two tables, but are not in the same order.  How to modify the query so that it finds the matching record between  the two tables?
    Here is my code
      loop at itab_A.
          read table itab_B with key
                 employeenumber = itab_zmgr_training-empid binary search.
              if sy-subrc = 0.
    Any thoughts?
    Thanks,
    Sk
    Here I am getting the return value as '4'

    I have tried all that, but why is it that while reading a record from itab_B I see no record in the header line,
    Let me paste the entire code of what I am trying to achieve.
    data: itab_A like A occurs 0 with header line.
    data: itab_B like B occurs 0 with header line.
    some validation.....
    loop at itab_A.
    read table  itab_B with key  employee number = empid binary search.
      if sy-subrc =0.
    per form validation.
    else.
    perform validation.
    endif.
    endloop.
    My observation: while reading recrod from itab_B the header line is clear, no record is read here hence sy-subrc = 0.
    How can I correct this? Since it is not reading any record from itab_B no matching record is found.
    Any thoughts would be really helpful.
    Thanks,
    Santhosh.

  • Using JOIN to two tables to find records NOT in second table...

    I always seem to get these types of queries wrong and need a bit of help:
    I have a table of relationships called MARKETER_ACCOUNT and another table of pending relationships called ACCOUNT_ENROLLMENT.
    I have accounts that can be enrolled with a MARKETER. They enter the ACCOUNT_ENROLLMENT table as Pending and on a certain Effective Date the go active and then move to the MARKETER_ACCOUNT table. If a relationship ends, it can end by either enrolling with another Marketer or if no pending enrollment they are assumed to be going back to the parent company.
    So I want to find all the records that came back to the parent company (i.e. they should not have a pending record in the ACCOUNT_ENROLLMENT table but have an end date in the MARKETER_ACCOUNT table:
    Select * from MARKETER_ACCOUNT M
    where M.account_no NOT IN (select E.account_no from ACCOUNT_ENROLLMENT E
    where E.effective_date between '08-DEC-2009' and '07-JAN-2010')
    and M.expiration_date between '08-DEC-2009' and '07-JAN-2010';
    This query is still running and I'm not sure where I am going wrong. I want to say GIVE me all the accounts in the MARKETER_ACCOUNT table that have an expiration date between '12/07/2009' and '01/07/2010' and where those accounts are NOT pending in the ACCOUNT_ENROLLMENT table where the effective date is for the same period.
    Any help would be greatly appreciated.
    Sean

    You could possibly try the NOT EXISTS version of the query:
    SELECT  *
    FROM    MARKETER_ACCOUNT M
    WHERE   NOT EXISTS
                    SELECT  NULL
                    FROM    ACCOUNT_ENROLLMENT E
                    WHERE   M.ACCOUNT_NO = E.ACCOUNT_NO
                    AND     E.EFFECTIVE_DATE BETWEEN TO_DATE('08-DEC-2009','DD-MON-YYYY') AND TO_DATE('07-JAN-2010','DD-MON-YYYY')
    AND     M.EXPIRATION_DATE BETWEEN TO_DATE('08-DEC-2009','DD-MON-YYYY') AND TO_DATE('07-JAN-2010','DD-MON-YYYY');Otherwise check out these threads:
    {message:id=1812597}
    {thread:id=863295}

  • How to find the longest record in a table?

    Hello,
    Is there a function to find the longest record in a table? Or is there a data dictionary that would tell you which record contains the longest data?
    I have a table with five columns and one million records. I want to find the record (5 columns combined) with the longest data. Thank you.

    Dear watson2000!
    The function "VSIZE" tells you the number of bytes in the internal representation of a column which means the size of a value within a column. An example of vsize can be found here:
    [http://www.adp-gmbh.ch/ora/sql/vsize.html]
    So I think you should try it with this query to get the size of the longest record:
    SELECT MAX(VSIZE(column1)) +
           MAX(VSIZE(column2))  +
           MAX(VSIZE(column3))  +
           MAX(VSIZE(column4))  +
           MAX(VSIZE(column5)) AS "Maximum Row"
    FROM your_table;To identify the longest record try like this:
    SELECT rowid
    FROM   your_table
    GROUP BY rowid
    HAVING  (MAX(VSIZE(column1)) +
             MAX(VSIZE(column2)) +
             MAX(VSIZE(column3)) +
             MAX(VSIZE(column4)) +
             MAX(VSIZE(column5))) = (SELECT MAX(VSIZE(column1)) +
                                          MAX(VSIZE(column2))  +
                                          MAX(VSIZE(column3))  +
                                          MAX(VSIZE(column4))  +
                                          MAX(VSIZE(column5))
                                   FROM your_table;)I hope that these two queries could be of help to you.
    yours sincerely
    Florian W.
    Edited by: Florian W. on 23.04.2009 20:53

  • Where is iTunes Match content stored on apple TV? I recently joined itunes match and it is working on my ios devices, but I can't find my music on Apple TV

    Where is iTunes Match content stored on apple TV? I recently joined itunes match and it is working on my ios devices, but I can't find my uploaded music on Apple TV

    It isn't actually stored anywhere, it needs to be downloaded from the internet before any content can be played.
    iTunes Match content is accessed through the 'Music' icon on the main screen though, it needs turning on though in settings first.

  • How to find last inserted record in the table.

    Version: Oracle 10g
    I have a table called "Manufacture" and 3 columns as mfno,itemname,quantity.
    How to find last inserted record in the table "Manufacture".
    As i come to know that Rowid is not result perfect results. Please provide your inputs.

    user13416294 wrote:
    Version: Oracle 10gThat is not a version. That is a product name. A version is 10.1.0.2 or 10.2.0.4, etc.
    I have a table called "Manufacture" and 3 columns as mfno,itemname,quantity.
    How to find last inserted record in the table "Manufacture".Not possible as your data model does not cater for it. That simple.
    If there is a need to determine some order or associate some time to an entity, then that should be part of the data model - and a relationship, or one or more attributes are needed to represent that information. Thus your data model in this case is unable to meet your requirements.
    If the requirements are valid, fix the data model. In other words - your question has nothing to do with Oracle and nothing to do with rowid, rowscn or other pseudo columns in Oracle. It is a pure data modeling issue. Nothing more.

  • Find - tables with largest number of records?

    Hi,
    I need to find tables with largest number of records. Any transaction show this details?
    aRs

    Go to transaction DB02,  then click on the button that reads,  "Space Statistics", the dialog box, click ok, leave the "*" for all tables,   In the next screen put your cursor in the appropriate column labeled as Rows and click the sort button.  Now you will see your biggest tables at the top of the list.
    Regards,
    Rich Heilman

  • How to find the unmatched records between the two tables?

    We have to tables in SQL Server database.
    But unable to find the unmatched record betwwen these two tables.
    There 12 records are unmatched. But unable to get those records.
    I am using the below sql:
    select c1,c2,c3,c4,c5,c6 from Table1
    except select c1,c2,c3,c4,c5,c6 from Table2
    Please help. Thanks in advance.

    Hi knra,
    Please try the following codes:
    select c1,c2,c3,c4,c5,c6 from Table1 t1
    where not exists
    (select * from Table2 t2
    where t1.c1= t2.c1
    and t1.c2= t2.c2
    and t1.c3= t2.c3
    and t1.c4= t2.c4
    and t1.c5= t2.c5
    and t1.c6= t2.c6)
    Best Regards,
    Allen Li
    Allen Li
    TechNet Community Support
    This is what I would've said, but it would make life much easier for you if you have a reference or ID for each entry, this is why we use keys.

  • Function module for finding number of records in table

    Do we have any function module to find number of records in a table?
    Thanks,
    Shweta

    use the EM_GET_NUMBER_OF_ENTRIES. You just have to write your table name into the input table IT_TABLES in the TABNAME field

  • How to insert a record for tables joined

    Hi,
    first of all I've been learning Forms for few days.
    I've created a simple form where I report the columns from two tables joining them.
    Once I've entered a new list of values in the fields of the form, how can I insert them in the two tables? What I've done, but it doesn't work, is to set "Insert Procedure Name" of the data block = to my procedure compiled in "Program Units" hoping that once pushed the "Execute Query" button in the running form worked, but it didn't work.
    Any advice for the better way to how execute an insert in one or more tables with the content of the fields of the form?
    My versions are: database 10gXE and forms 10.1.2.0.2
    Thanks in advance!

    Hi,
    Create two data blocks using create data block wizard and join it with a relation. Display the fields you want and form will automatically save your updates in the both tables.
    When there is a join you can not update all the fields.
    Regards
    Yoonas
    Edited by: yoonus on Oct 3, 2012 1:04 AM

  • Oracle 10g - To find the corresponding record for a certain row

    Hi all,
    The scenario is like this - Suppose I've got a table with 100+ columns. For a certain row inside, I need to find its corresponding record which is in the same table. The way how I define "corresponding" here is - these two rows should be identical in all attributes but only different in one column, say "id" (primary key).
    So how could I achieve this? What I can think of is to fetch all columns of the first row into some pre-defined variables, then use a cursor to loop the table and match the values of the columns of each row to those variables. But given that we've got 100+ rows in the table, this solution doesn't look practical?
    Any advises are greatly appreciated. Thanks.

    something to play with as Solomon suggested (use some other string aggregation technique if you're not 11g yet)
    you'll have to adjust the column_list accordingly
    select 'select ' || column_list ||
           ' from ' || :table_name ||
           ' group by ' || column_list ||
           ' having count(*) > 1' the_sql
      from (select listagg(column_name,',') within group (order by column_id) column_list
              from user_tab_cols
             where table_name = :table_name
           )Regards
    Etbin
    Edited by: Etbin on 25.12.2011 16:53
    Sorry, I'd better leave the forum: the title says you're 10g :(
    Providing a link for replacing listagg: http://www.sqlsnippets.com/en/topic-11787.html

  • How to find count of records based on batch wise.

    Hi All,
    We are working on OWB10gr2. I would like share one requirement. Any one can suggest me how to do it and which is the best way to get accurate information.
    We have 2 schemas’ like nia_src and nia_tgt. currently we are moving data from nia_src to nia_tgt for that reason we implemented some mappings based on requirement. In my source schema (nia_src) having 100 tables with data and similar structure replicated in target schema (nia_tgt).
    In source schema every table having one date field for which record is inserted and based on that field we can find how may records are inserted on particular table ,particular time.
    Same like target also maintaining date fields for tracking purposes.
    We have done some mappings and scheduled also. Every day we are into the target with incremental data. All are working fine not any issues.
    I wanted to know how many records inserted, updated, merged for particular batch
    How can we find?
    Can we find exact information in OWB_REP_OWNER schema on WB_RT_AUDIT?.
    For tracking purposes I need to find those values how many records are available in
    Source table and how many records are populated to the target schema?
    How to find schedule batch count of records table wise for batch wise?
    Please suggest me any one on this.
    thanks and regards,
    venkat.

    RE: based on pre operator can we find count of records. if yes how to do it.
    No, you cannot tell from the pre- operator how many records will be inserted or updated into a mapping. How could you? The mapping hasn't run yet!
    At best (if you have simple mappings with single targets) you could come up with a strategy to have the pre-mapping procedure aware of it's package name, then select from user_source for that package body until you find that first cursor used for the row-based processing, copy the cursor into a local variable, and then execute immediate select count(*) from that cursor definition. But still, all that would get you is the number of rows selected - not inserted / updated / errored etc.
    A post-mapping procedure that is aware of the package name will, however, run prior to package exit so that the package spec is still in memory so you can access the public variables in the package spec. We do that in our standard post-mapping procedure
    CREATE OR REPLACE procedure erscntrl_finalize_prc(
                           p_process_id     in  number,
                           p_process_name   in  varchar2)
    as
          l_numread      number         := 0;
          l_numInserted  number         := 0;
          l_numUpdated   number         := 0;
          l_numMerged    number         := 0;
          l_owb_audit_id number         := 0;
          l_owb_status   number         := 0;
          sqlStmt        varchar2(2000) :=
                               'begin '||
                               '  :1 := '||p_process_name||'.get_selected; '||
                               '  :2 := '||p_process_name||'.get_inserted; '||
                               '  :3 := '||p_process_name||'.get_updated; '||
                               '  :4 := '||p_process_name||'.get_merged; '||
                               '  :5 := '||p_process_name||'.get_audit_id; '||
                               '  :6 := '||p_process_name||'.get_status; '||
                               ' end;';
    begin
          -- we use dynamic SQL to return required audit field values.
          --  This allows us to alter which values we need at a later date
          --  without impacting the deployed mappings.
        execute immediate sqlStmt
        using   out l_numread,   out l_numInserted,  out l_numUpdated,
                out l_numMerged, out l_owb_audit_id, out l_owb_status;
      -- then execute our own logging package.
       owb_mapping_log_pkg.finalize(
                           p_process_id,
                           p_process_name,
                           l_numread,
                           l_numInserted,
                           l_numUpdated,
                           l_numMerged,
                           l_owb_audit_id,
                           l_owb_status
    end;
    /However, even in this case bear in mind that if you run the mapping in set-base mode, all Oracle returns is the number merged which does not give values for the inserted and updated counts. If you really need those values you need to either a) run in row-based mode or row-based-target-only, or come up with some custom queries. For example, in your pre-mapping do a select count(*) from your_target_table, then run the mapping, then get the number merged, then do another select count(*) from your_target_table. With these values and basic math you could tell the number inserted by the growth in the table, and the rest of the number merged must have been updates.
    That being said, if you are playing with dimensions as large as most of the ones I am - there is no bloody way that you want to do two select count(*) statements on each run without a really, really good reason.....
    Cheers,
    Mike

  • Performance on large table join return top result

    Hi all,
          We're execution POC on a public sector customer.
          In their enviorement, they have a SQL like this:
          select top 100 * from table A join table B on A.party_id=b.party_id  ( No filter here)
          table A: about 40Million records,    table B: 1.2 billion records,   it's in a 6 nodes cluster. each table parttion by HASH 40 across all nodes.
          It take 11 mins to finish this SQL, and query nodes memory almost used up.
          While other Database, such as Oracle can use hint first_rows, this wll return top 100 result without query on all table. It only takes serval seconds on other DB.
          My questions here, does this any Statement optimization in HANA can return top 100 result as quickly as it can.

    Sam I suggest you take advice from the HANA COE if you have a PoC in play. With this setup, you need advice from a HANA expert.
    I can't see any realistic business scenario where that query would be used, so whoever is asking for it is just asking it as a science experiment. There are no hints like first_rows on the HANA DB because HANA is optimized for real-world scenarios.
    You could always model it as:
    select top 100 * from table A join (select top 10000000 * from table B) B on A.party_id=b.party_id
    But that's just as fake as your first question (though it will respond much faster...). Best is to instead find the real queries that the business will ask.
    However if you have a 6 node cluster then your design is all off. HASH 40 is far too many partitions, you need 6 - one for each node. Also for your master data, don't use a partition. Instead use the REPLICA functionality ALTER TABLE - SAP HANA SQL and System Views Reference - SAP Library
    For the scenario you describe, all queries should be sub-second. Check Abani's blog for some interesting advice.
    Advanced Modelling: Retail Use Case

Maybe you are looking for