by comparing the number of records in the table child

I have 3 tables below:

Table 1 - Parent Table
Table table2 child - who has the number of records for each record in Table1 (row_id in Table1 corresponds to par_row_id in Table2)
Child table table 3 - has that number of records for each record in Table1 (row_id in Table1 corresponds to par_row_id in table 3)

I would like to run an Oracle SQL query to identify all the separate row_id on Table1, for which the number of child records in Table2 does not match the number of child records in Table3

I ask for help to identify the best way to identify these records in the parent table. If possible an example too, please.

Thank you

Published by: cseshan on February 3, 2013 01:35

All by adding the simplest example (with the sample data that you were supposed to provide) below

create table table1(row_id number primary key);

create table table2(par_row_id number references table1);

create table table3(par_row_id number references table1);

insert into table1 values(1);
insert into table2 values(1);
insert into table3 values(1);

insert into table1 values(2);
insert into table2 values(2);
insert into table2 values(2);

insert into table1 values(3);
insert into table3 values(3);
insert into table3 values(3);

insert into table1 values(4);
insert into table2 values(4);
insert into table2 values(4);
insert into table2 values(4);
insert into table3 values(4);
insert into table3 values(4);

commit;

with child1_cnt as
(
  select par_row_id,count(*) cnt
  from table2
  group by par_row_id
),
child2_cnt as
(
  select par_row_id,count(*) cnt
  from table3
  group by par_row_id
),
all_counts as
(
  select p.row_id,nvl(c1.cnt,0) c1_cnt,nvl(c2.cnt,0) c2_cnt
  from table1 p
   left outer join child1_cnt c1
     on ( p.row_id = c1.par_row_id )
   left outer join child2_cnt c2
     on ( p.row_id = c2.par_row_id )
)
select row_id,c1_cnt,c2_cnt
from all_counts
where c1_cnt != c2_cnt;

ROW_ID C1_CNT C2_CNT
------ ------ ------
     2      2      0
     4      3      2
     3      0      2 

Tags: Database

Similar Questions

  • Count the number of records in all tables with a querry URGENT please...

    I downloaded ORACLE 9I DATA DUMP in my computer. There are number of tables. I need to know the tables with records more of say 100 or on the go. Is it possible... If so kindly let me know.

    Second, the database that I downloaded, I discovered that Audit_ trial was not activated by cooking
    ' select name, value of v$ parameter.
    where name like '% verification' and it turned out to be false.
    Can we assume that the database in the server as the audit_trial has been disabled...

    Published by: user1287492 on October 21, 2009 09:07

    Hello

    Place the table name with quotes. Maybe that's the problem.

    Try this code

    select table_name,
     to_number(
     extractvalue(
     xmltype(
     dbms_xmlgen.getxml('select count (*) c from "'||table_name||'"' ))
     ,'/ROWSET/ROW/C')) COUNT
     FROM USER_TABLES
     where iot_type IS NULL
    /
    

    SS

  • Compare the table structures in different schemas. help please

    Hi all

    I have a question...


    I have pictures on different schemas something like


    Diagram A
    -------------
    Table 1
    Table 2
    Table 3


    Diagram B
    ------------
    Table 1
    Table 2
    Table 3


    Now situation is table 1 and table 2 will have a similar structure or table 1 in Figure B will have additional columns.

    like so... on... for all other tables...

    example!
    Schema A:
    
    Desc Table 1;
    
    Name                                  Type            Null
    -------------                             -------             -------
    No                                Number            Notnull
    Name                           Varchar2(10)     Not null
    Fee                              Number (10,2)   Not null
    
    
    
    Scheam B;
    
    Desc Table1;
    
    Name                                  Type               Null
    -------------                             -------             -------
    DX_No                                Number            Notnull
    DX_Name                           Varchar2(10)      Not null
    DX_Fee                              Number (10,2)   Not null
    comments                          Varchar(2)        
    Now I need to write some sort of procedure for the thing to compare these tables which are in different in the column names in the schema and get it had exported to an Excel sheet.
    and here it's first three columns SHOULD BE CONSIDERED AS SAME even if the DX_ prefix are from the REST OF the PART OF THE COLUMN NAME IS SAME.

    and the same way commit coloumn new schema B only... So it should be noted that excel sheets...

    I don't know how the ADO or SQL Developer handle this... Is there any plsql block that I can write to do it...

    Thanks in advance...

    Comparison of columns on all the tables in the two schema

    select tc1.owner,tc1.TABLE_NAME,tc1.COLUMN_NAME,
           tc2.owner,tc2.TABLE_NAME,tc2.COLUMN_NAME
    from
      all_tab_columns tc1
      full outer
      join all_tab_columns tc2 on &user1=tc2.owner
                               and tc1.TABLE_NAME=tc2.TABLE_NAME
                               and tc1.COLUMN_NAME like '%'||tc2.COLUMN_NAME
    where
    tc1.owner=&user2
    and tc1.TABLE_NAME in (
                          select t1.table_name
                          from all_tables t1
                          join all_tables t2 on t2.owner=&user2 and t1.table_name=t2.table_name
                          where t1.owner=&user1)
    

    Published by: xtender on 06.11.2010 12:43

  • Compare the table and out of the common elements

    Hello

    Can you help me to compare the two tables in the attached file in VI and out the elements common to the two tables?

    Thank you

    hiNi

    There is no wire table through the False State in the structure of your business.  Use the default value if unwired is selected for this tunnel. the default value for this tunnel is an array of empty strings.  Run the wire straight through, and Bob's your father's brother.

  • Get number of records in all tables

    Hello

    I try to get the number of records from all the tables by using a dynamic query. I don't know how to put the placeholder value. I tried the code below.

    SET SERVEROUTPUT SIZE 1000000
    DECLARE
    CURSOR table_list
    IS
    Select OBJECT_NAME from user_objects
    where object_type in ('TABLE')
    and object_name not like '% AUDIT_DDL % '.
    AND object_name not like 'MD_ % '.
    AND object_name not like "EXT_ %.
    AND object_name not like 'STG_ % '.
    AND object_name not like '% SYS_ ".
    AND object_name not like '% TMP_ ".
    AND object_name not like 'TEMP_ %.
    order by 1;
    v_count NUMBER: = 0;
    query_str VARCHAR2 (1000);
    BEGIN
    FOR table_name IN table_list
    LOOP
    query_str: = ' SELECT COUNT (1) FROM "| table_name. Object_name;
    dbms_output.put_line (query_str);
    dbms_output.put_line (' Table name:' | table_name.) OBJECT_NAME);
    v_count: = run immediately query_str;
    dbms_output.put_line (' Table name:' | table_name.) OBJECT_NAME | ', County'. v_count);
    END LOOP;
    END;

    I know that I am doing wrong in the "BOLD" lines. But do not know how to fix it. Help, please. Thanks in advance.

    Change:

    v_count:= execute immediate query_str;
    

    TO

    execute immediate query_str into v_count;
    
  • Compare the table

    Hello:

    I need to compare two index table, to see the file please, and if there is no specific line or column changes then indicate any number of line and colunm

    How can I do in labview

    Thank you

    Brice

    I have created 2 examples in the attached VI.

    The loop at the top of the page search for any line that indicates the index of the first inconsistency and incompatibility.

    The loop at the bottom search all mismatched line/column, regardless of having previously found it an inconsistency in the same row.  It gives you a complete list.

    The example (s) should give you an idea of how to do to implement your solution.

  • Try to count the number of records where some columns are met

    Using SQL in Toad against an Oracle table, I'm trying to count the number of records in a table where some columns are filled. For
    example, I need to know the total number of records from a table where

    ln_stop_cd is not null - 65000 records
    ln_process_cd is <>- 25000 0 records
    ln_opt_cd is not null - 7500 records

    and the record_type = "A".

    I know that the total individual records as described above. However, I do not know how
    the SQL to show for record_type = 'A' the total count is 97500 records. Any help would be
    appreciated.

    Hello

    It seems that the problem is that the indictment may overlap; You can count the same rank 0, 1, 2 or 3 times, depending on whether 0, 1, 2 or 3 of these 3 independent conditions are met.

    Try something like this:

    SELECT     COUNT (in_stop_cd)
          + COUNT (CASE WHEN in_process_cd  != 0        THEN 1 END)     -- this site doesn't like the other inequality operator
          + COUNT (in_opt_cd)        AS grand_total
    FROM    table1
    WHERE     dt          >= TO_DATE ('01-JUN-2011', 'DD-MON-YYYY')
    AND     dt          <  TO_DATE ('02-JUN-2011', 'DD-MON-YYYY')
    AND     record_type       = 'A'
    ;
    

    You want to make charges without making 3 assists separated through the table. The problem is the 3 original requests had slightly different WHERE clauses.
    Put these conditions in the WHERE clause and make other conditions apply only to the individual counties, either with a column instead of *, or using a CASE statement that contains the condition that applies only to the column.

    Also, the date is not a column name good. Do not compare the DATEs to strings. If your column is really a DATE and you want to ignore the hours, the minutes and seconds, then the best way is to compare the column with 2 other DATES, as shown above. It's more coding, but it is more efficient and more reliable.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
    You don't have to display a large amount of data. You can probably give an excellent example of this problem with only 5 or 10 rows of sample data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

    Published by: Frank Kulash, August 4, 2011 11:27

  • Query to get the number of records

    Hi all

    I would get the number of records in all tables in a schema. I don't want to run select count (*) of < table name >, because there are nearly 400 paintings. So I need a generic quqeyr that will give the recordcount for all tables.

    Experts good help yourslef to tide me over.

    Do you need a specific account? Or an approximate count is sufficient? Do you use the RBO and CBO?

    If you use the CBO and want an approximate count, column NUM_ROWS USER_TABLES should give you what you want.

    If you need a specific account, you will have full scan all tables. You can automate this process via SQL dynamic, i.e.

    DECLARE
      l_cnt INTEGER;
    BEGIN
      FOR x IN (SELECT table_name FROM user_tables)
      LOOP
        EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || x.table_name INTO l_cnt;
        dbms_output.put_line( 'Table ' || x.table_name || ' has ' || l_cnt || ' rows.' );
      END LOOP;
    END;
    

    Lawrence was also a [solution based on XML Query | http://laurentschneider.com/wordpress/2007/04/how-do-i-store-the-counts-of-all-tables.html] to count all the rows in each table in a schema.

    Justin

  • Removal of the records in the tables

    Oracle 11g - Windows Server 2008

    After you delete a large number of records in a table, I think I should reorg or do something for it to be fine.

    The table name is def_lancs.
    I deleted records over 1 million of this table. It now has 2 million records.
    What commands can I use to put this table in order?

    Eduardo

    788042 wrote:
    Hello

    In the SQL databases after you delete a large number of rows in a table, we have a very large space with no information.
    Lines are deleted, but the space using these lines is always there.
    We must therefore use retractable in order to remove this empty space.
    After that, the present table less space and performance improves.

    I don't know how it works in Oracle.
    But as you tell me to relax and have a cup of coffee, I think that the Oracle databases do this automatically.
    Is this true?

    Eduardo

    No, they don't to shrink automatically and free up space, but usually no need to. Oracle manages to somehow fundamentally and profoundly different than SQLServer. Best practices in may very well be the worst practices in the other. In oracle, allocation and deallocation extensions are quite expensive activity. If I delete a bunch of lines that normally I guess at some point the space the lines take it will take once again, so I'm not going to Oracle through the effort to free the space just for having to recover again. At least not outside has demonstrated clearly need to do.

  • difference in number of records

    Hi all

    I'm trying to get a number of records in this table and I get 2 different output, select count (*) shows 68560 and after update stats NUM_ROWS watch 68323, can someone help me figureout why the difference.

    SQL > select count (*) in the dcepcd;

    COUNT (*)
    ----------
    68560

    SQL > select num_rows in dba_tables where table_name = 'DCEPCD ';

    NUM_ROWS
    ----------
    68323

    Thank you / JJ

    864653 wrote:
    Hi all

    I'm trying to get a number of records in this table and I get 2 different output, select count (*) shows 68560 and after update stats NUM_ROWS watch 68323, can someone help me figureout why the difference.

    SQL > select count (*) in the dcepcd;

    COUNT (*)
    ----------
    68560

    SQL > select num_rows dba_tables where table_name = 'DCEPCD ';

    NUM_ROWS
    ----------
    68323

    Thank you / JJ

    When you gather stats, Oracle is probably just sampling the data. This means that Oracle would be approximately the number of lines, not give you the actual number of lines. Please read the documentation on DBMS_STATS:
    http://docs.Oracle.com/CD/E11882_01/AppDev.112/e25788/d_stats.htm

    Here is a relevant excerpt:
    Table 141-39 GATHER_DATABASE_STATS procedure parameters
    estimate_percent
    Percentage of rows estimate (NULL means calculates): the valid range is [0.000001,100]. Use the constant DBMS_STATS. AUTO_SAMPLE_SIZE to Oracle to determine the size of the sample appropriate for good statistics. It is the default value. The default value can be changed using the SET_DATABASE_PREFS procedure SET_GLOBAL_PREFS procedure, procedure SET_SCHEMA_PREFS and SET_TABLE_PREFS procedure.

    Summary: You should get the actual number of lines if gather you stats with estimate_percent => NULL. Be aware that Oracle does not by default for performance reasons.

  • How to count records in a Table or form

    I have a Table that went to my context in the form of ADOBE.  I need to be able to count the number of records in this table.  I didn't see the actual table in this case on the shape, I just need than the number of records.  And, I need to do this using JavaScript and I don't know anything about Java (although if FormCalc works I can use this as well).     My Tablename is a customer and in my form, it's in the hierarchy data .main .customerbody .customerline.  If I have to have these data in the table on the form, then I can just put it on the form and invisiible.  I really need to count the number of records well and return this # to a text field in the form.

    Does anyone know how to do this and can you give me an example is Java for FormCalc?  The more specific you can be with the example script best for this beginner.  THX!

    I was no longer complicate things, and I found a simple answer to my problem.  I do this on SAP with Livecyle form so here's what I did

    In the Interface, I created a global variable called gv_count.  I then added, during the initialization of form Interface, copy the following code:

    describe the table reservations LINES gv_count.

    And then, in the form, I had just to add gv_count to the context and how link this to the field I want to do the count

  • By comparing the number of different records of different tables of databases with Production DB

    Hello

    I updated 6 paintings of DEVEOPMENT and QA database production databases.

    According to the request of the customer, I want to create a stored procedure to compare the number of tables with the number of Production DB records.

    Could you please suggest me a better way to do this.

    example: select count (*) in the emoployee@Devlopment_Database.  -I want to check with select count (*) from employee@Procuction_Database

    Select count (*) in the emoployee@QA_Database.                 -I want to check with select count (*) from employee@Procuction_Database

    Thank you

    Vincent

    If so, your question is asking how to create a database link?

    CREATE THE DATABASE LINK

  • number of records per page total records compared to the BI Publisher

    Hello

    In the tool Oracle BI Publisher, please can you me how to display the total number of records in a report? as well as how to display the number of records per page?

    Thanks for your help

    Concerning
    Garza

    check the Inbox. passed the updated model

  • I can capture the number of records retrieved by page in the Apex Interactive report?

    Hello

    I have a requirement in which I need to conditionally hide / show button on an interactive report.

    To achieve this, I need to compare the number of reows read by v/s lines page number to be

    read as specified by the user (defined using Actions-> lines per page).

    Please help me to know if there is a way to capture these 2 values:

    1. number of records retrieved in a report by page

    2. the value set by the user for the number of rows must be extracted (Actions--> lines per page).

    Quick help is really appreciated. Thank you.

    Hello

    You can use by jariola in his blog

    http://dbswh.webhop.NET/Apex/f?p=blog:read:0:article:41900346848694

    Thank you

    Benjamin

  • The number of records Oracle allows?

    Does anyone have an idea on the maximum number of lines/records which may allow Oracle on a single table. My organization compared the RDBMS and this question came up. Would you know the comparison with DB2 and also Microsoft SQL 2005?

    Thanks in advance

    pachemwi

    http://download.Oracle.com/docs/CD/E11882_01/server.112/e10820/limits.htm

Maybe you are looking for

  • Updated 15-f039wm HP computer laptop memory

    I just bought a HP 15-f039wm laptop which comes with 4 GB of memory.  I would like to increase the memory up to 8 GB.  Can I replace the 4 GB key with a 8 GB memory stick?  What is the maximum memory that will support this laptop.  Thank you very muc

  • Sharing network and permissions - Windows 2012R2

    I am trying to implement the actions of network and could use help. My plan is to create the following directory structure: D:\Shares$ In the D:\Shares$ directory would be: D:\Shares$\Users D:\Shares$\Departments D:\Shares$\Shared D:\Shares$\Temp The

  • Following updates KB968930 and KB982670 cannot start

    I have an Acer Aspire 5738Z Windows Vista Edition Home Premium After the update of the foregoing, I now have a screen (not brilliant blue!).  Pressing the button for 10 seconds to turn off and then turn on new give me the Green blocks little followed

  • Through Windows Firewall ' do an outgoing connection on Port 21 '

    "How an outgoing connection on Port 21". I'm trying to update a web 'edition' - site.and there comes to expression, they used to help me solve my problem.  All information and data is OK on their end apparently

  • 15 - K167CL: ENVY 15-K167CL bays

    This laptop have more connection to readers - like a HARD drive and SSD?