Query to find duplicate records (Urgent!!!!)

Hi,
I have a to load data from a staging table to base table but I dont want to load data already present in the base table. Criteria to identify the duplicate data is thorugh a field say v_id and status_flag. If these two are the same in both staging and base table then that record must be rejected as a duplicate record.
Kindly help me with the SQL which i need to use in a Procedure.
Thanks

Hello
Another alternative would be to use MINUS if the table structures match:
--Source rows the first 5 are in the destination table
SQL> select * from dt_test_src;
OBJECT_ID OBJECT_NAME
101081 /1005bd30_LnkdConstant
90723 /10076b23_OraCustomDatumClosur
97393 /103a2e73_DefaultEditorKitEndP
106075 /1048734f_DefaultFolder
93337 /10501902_BasicFileChooserUINe
     93013 /106faabc_BasicTreeUIKeyHandle
     94929 /10744837_ObjectStreamClass2
    100681 /1079c94d_NumberConstantData
     90909 /10804ae7_Constants
    102543 /108343f6_MultiColorChooserUI
     92413 /10845320_TypeMapImpl
     89593 /10948dc3_PermissionImpl
    102545 /1095ce9b_MultiComboBoxUI
     98065 /109cbb8e_SpanShapeRendererSim
    103855 /10a45bfe_ProfilePrinterErrors
    102145 /10a793fd_LocaleElements_iw
     98955 /10b74838_SecurityManagerImpl
    103841 /10c906a0_ProfilePrinterErrors
     90259 /10dcd7b1_ProducerConsumerProd
    100671 /10e48aa3_StringExpressionCons
20 rows selected.
Elapsed: 00:00:00.00
--Destination table contents
SQL> select * from dt_test_dest
  2  /
OBJECT_ID OBJECT_NAME
    101081 /1005bd30_LnkdConstant
     90723 /10076b23_OraCustomDatumClosur
     97393 /103a2e73_DefaultEditorKitEndP
    106075 /1048734f_DefaultFolder
     93337 /10501902_BasicFileChooserUINe
Elapsed: 00:00:00.00
--try inserting everything which will fail because of the duplicates
SQL> insert into dt_test_dest select * from dt_test_src;
insert into dt_test_dest select * from dt_test_src
ERROR at line 1:
ORA-00001: unique constraint (CHIPSDEVDL1.DT_TEST_PK) violated
Elapsed: 00:00:00.00
--now use the minus operator to "subtract" rows from the source set that are already in the destination set
SQL> insert into dt_test_dest select * from dt_test_src MINUS select * from dt_test_dest;
15 rows created.
Elapsed: 00:00:00.00
SQL> select * from dt_test_dest;
OBJECT_ID OBJECT_NAME
    101081 /1005bd30_LnkdConstant
     90723 /10076b23_OraCustomDatumClosur
     97393 /103a2e73_DefaultEditorKitEndP
    106075 /1048734f_DefaultFolder
     93337 /10501902_BasicFileChooserUINe
     89593 /10948dc3_PermissionImpl
     90259 /10dcd7b1_ProducerConsumerProd
     90909 /10804ae7_Constants
     92413 /10845320_TypeMapImpl
     93013 /106faabc_BasicTreeUIKeyHandle
     94929 /10744837_ObjectStreamClass2
     98065 /109cbb8e_SpanShapeRendererSim
     98955 /10b74838_SecurityManagerImpl
    100671 /10e48aa3_StringExpressionCons
    100681 /1079c94d_NumberConstantData
    102145 /10a793fd_LocaleElements_iw
    102543 /108343f6_MultiColorChooserUI
    102545 /1095ce9b_MultiComboBoxUI
    103841 /10c906a0_ProfilePrinterErrors
    103855 /10a45bfe_ProfilePrinterErrors
20 rows selected.You could use that in conjunction with the merge statement to exclude all trully duplicated rows and then update any rows that match on the id but have different statuses:
MERGE INTO dest_table dst
USING(     SELECT
          v_id,
          col1,
          col2 etc
     FROM
          staging_table
     MINUS
     SELECT
          v_id,
          col1,
          col2 etc
     FROM
          destination_table
     ) stg
ON
     (dts.v_id = stg.v_id)
WHEN MATCHED THEN....HTH

Similar Messages

  • Help with Finding Duplicate records Query

    HI,
    I am trying to write a query that will find duplicate records/cases.
    This query will be used in a report.
    So, here are the requirements:
    I need to find duplicate cases/records based on the following fields:
    DOB, DOCKET, SENT_DATEI was able to do that with the following query. The query below is able to give me all duplicate records based on the Criteria above
    SELECT      DEF.BIRTH_DATE DOB,
               S.DOCKET DOCKET,
               S.SENT_VIO_DATE SENT_DATE, COUNT(*)
    FROM SENTENCES S,
                DEFENDANTS DEF
    WHERE      S.DEF_ID = DEF.DEF_ID
    AND       S.CASE_TYPE_CODE = 10
    GROUP BY  DEF.BIRTH_DATE, S.DOCKET, S.SENT_VIO_DATE
    HAVING COUNT(*) > 1;
    //I AM GOING TO CALL THIS QUERY 'X'Now, the information to be displayed on the report: defendants Name, DOB, District, Docket, Def Num, Sent Date, and PACTS Num if possible.
    The problem that I need help on is how to combine those queries together (what I mean is a sub query). the 'X' query returns multiple values. please have a look at the comments on the query below to see what I'm trying to achieve.
    here is the main query:
    SELECT      INITCAP(DEF.LAST_NAME) || ' ' || INITCAP(DEF.FIRST_NAME) || ' ' || INITCAP(DEF.MIDDLE_NAME) DEFENDANT_NAME,
            DEF.BIRTH_DATE DOB,
            TRIM(DIST.DISTRICT_NAME) DISTRICT_NAME,
            S.DOCKET DOCKET,
            S.DEF_NUM DEF_NUM,
            S.SENT_VIO_DATE SENT_DATE,
            DEF.PACTS_ID PACTS_NUM
    FROM      USSC_CASES.DEFENDANTS DEF,
            USSC_CASES.SENTENCES S,
            LOOKUP.DISTRICTS DIST
    WHERE      DEF.DEF_ID = S.DEF_ID
    AND      S.DIST_ID = DIST.USSC_DISTRICT_ID
    AND     S.CASE_TYPE_CODE = 10
    AND     S.USSC_ID IS NOT NULL
    AND // what i'm trying to do is: DOB, DOCKET, SENT_DATE IN ('X' QUERY), is this possible ??
    ORDER BY DEFENDANT_NAME; thanks in advance.
    I am using Oracle 11g, and sql developer.
    if my approach doesn't work, is there a better approach ?
    Edited by: Rooney on Jul 11, 2012 3:50 PM

    If I got it right, you want to join table USSC_CASES.DEFENDANTS to duplicate rows in USSC_CASES. If so:
    SELECT  INITCAP(DEF.LAST_NAME) || ' ' || INITCAP(DEF.FIRST_NAME) || ' ' || INITCAP(DEF.MIDDLE_NAME) DEFENDANT_NAME,
            DEF.BIRTH_DATE DOB,
            TRIM(DIST.DISTRICT_NAME) DISTRICT_NAME,
            S.DOCKET DOCKET,
            S.DEF_NUM DEF_NUM,
            S.SENT_VIO_DATE SENT_DATE,
            DEF.PACTS_ID PACTS_NUM
      FROM  USSC_CASES.DEFENDANTS DEF,
             SELECT  *
               FROM  (
                      SELECT  S.*,
                              COUNT(*) OVER(PARTITION BY DEF.BIRTH_DATE, S.DOCKET, S.SENT_VIO_DATE) CNT
                        FROM  USSC_CASES.SENTENCES S
               WHERE CNT > 1
            ) S,
            LOOKUP.DISTRICTS DIST
      WHERE DEF.DEF_ID = S.DEF_ID
       AND  S.DIST_ID = DIST.USSC_DISTRICT_ID
       AND  S.CASE_TYPE_CODE = 10
       AND  S.USSC_ID IS NOT NULL
      ORDER BY DEFENDANT_NAME;If you want to exclude duplicates from the query and do not care which row out of duplicate rows to choose:
    SELECT  INITCAP(DEF.LAST_NAME) || ' ' || INITCAP(DEF.FIRST_NAME) || ' ' || INITCAP(DEF.MIDDLE_NAME) DEFENDANT_NAME,
            DEF.BIRTH_DATE DOB,
            TRIM(DIST.DISTRICT_NAME) DISTRICT_NAME,
            S.DOCKET DOCKET,
            S.DEF_NUM DEF_NUM,
            S.SENT_VIO_DATE SENT_DATE,
            DEF.PACTS_ID PACTS_NUM
      FROM  USSC_CASES.DEFENDANTS DEF,
             SELECT  *
               FROM  (
                      SELECT  S.*,
                              ROW_NUMBER() OVER(PARTITION BY DEF.BIRTH_DATE, S.DOCKET, S.SENT_VIO_DATE ORDER BY 1) RN
                        FROM  USSC_CASES.SENTENCES S
               WHERE RN = 1
            ) S,
            LOOKUP.DISTRICTS DIST
      WHERE DEF.DEF_ID = S.DEF_ID
       AND  S.DIST_ID = DIST.USSC_DISTRICT_ID
       AND  S.CASE_TYPE_CODE = 10
       AND  S.USSC_ID IS NOT NULL
      ORDER BY DEFENDANT_NAME;SY.

  • How to create an activity when system finds duplicate record!!

    Hi CRM Experts,
    I am working on CRM 5.0. We are uploading contact details to CRM through ELM.
    Here My queries are:
    1) How to create An Activity when system finds duplicate record?
    2) By using ELM we can create BP with Activities and BP with Leads. But Here, my scenario is we have to Create BP with leads and Acivities. can any one help me on these areas?
    Thank in Advance.
    Sree

    Hi Sree,
    I can help you with your first query.
    When the system finds a duplicate record then either the system stops working further or proceeds with the error free record.
    So once the duplicate entry is found only the first record will be considered and not the second or the duplicate record.
    Regards,
    Rekha Dadwal
    Kindly reward with points if usefull !!!!

  • Query to find duplicate lows

    Dear all,
    i have a table
    create table attendance(dept_id number,att_date date,att_kind);
    all the three columns i want to put as a primary key,
    there is a data in the table.
    how would i check through a query that the table has duplicate values or not?
    in other words , i want to query just the duplicate records so i delete them manually.
    thanks & Regards

    You do it like this.
    SQL> select * from attendance;
       DEPT_ID ATT_DATE    ATT_KIND
           100 20-FEB-10          1
           100 20-FEB-10          1
           200 20-FEB-10          1
           200 20-FEB-10          2
    SQL> create table exceptions(row_id rowid,
      2   owner varchar2(30),
      3   table_name varchar2(30),
      4    constraint varchar2(30));
    Table created.
    SQL> alter table attendance add constraint att_pk  primary key (dept_id, att_date, att_kind) exceptions into exceptions;
    alter table attendance add constraint att_pk  primary key (dept_id, att_date, att_kind) exceptions into exceptions
    ERROR at line 1:
    ORA-02437: cannot validate (TEST.ATT_PK) - primary key violated
    SQL> select * from exceptions;
    ROW_ID             OWNER                          TABLE_NAME                     CONSTRAINT
    AAAC/yAAGAAAAAOAAB TEST                           ATTENDANCE                     ATT_PK
    AAAC/yAAGAAAAAOAAA TEST                           ATTENDANCE                     ATT_PK
    SQL> select * from attendance where rowid in (select row_id from exceptions);
       DEPT_ID ATT_DATE    ATT_KIND
           100 20-FEB-10          1
           100 20-FEB-10          1
    SQL>Asif Momen
    http://momendba.blogspot.com

  • Query to find duplicates, update appropriately, delete one of those duplicate

    I have duplicate rows in below given table and need to cleanse it by observing another duplicate record.
    I'd like to know various ways to achieve it. (I'm confused I should use UPDATE.. (CASE WHEN.. THEN).. or MERGE or something else)
    Please find below DDL/DML.
    create table MyTable
    PKey int identity(1,1),
    CustID int,
    FirstName varchar(10),
    LastName varchar(10),
    Main varchar(10),
    Department varchar(10)
    Insert into MyTable
    select 101, 'aaa','bbb','VM','Marketing' union
    select 101, '', '','','' union
    select 102, '', 'yyy', 'Main', 'Marketing' union
    select 102, 'xxx','','','' union
    select 103, 'ppp', 'qqq', '', 'HR' union
    select 103, '', '', 'MF', '' union
    select 104, 'mmm', 'nnn', 'f', 'dept'
    select * from mytable
    --PKey CustID FirstName LastName Main Department
    --2 101 aaa bbb VM Marketing
    --3 102 xxx yyy Main Marketing
    --6 103 ppp qqq MF HR
    --7 104 mmm nnn f dept
    Cheers,
    Vaibhav Chaudhari

    Hi Vaibhav,
    Manu's has copied as a part of the below code.
    create table MyTable
    PKey int identity(1,1),
    CustID int,
    FirstName varchar(10),
    LastName varchar(10),
    Main varchar(10),
    Department varchar(10)
    Insert into MyTable--(CustID,FirstName,LastName,Main,Department)
    select 101, 'aaa','bbb','VM','Marketing' union
    select 101, '', '','','' union
    select 102, '', 'yyy', 'Main', 'Marketing' union
    select 102, 'xxx','','','' union
    select 103, 'ppp', 'qqq', '', 'HR' union
    select 103, '', '', 'MF', '' union
    select 104, 'mmm', 'nnn', 'f', 'dept'
    SELECT * FROM MyTable;
    ;WITH cte AS
    SELECT DISTINCT
    MAX(PKey) PKey,
    CustID,
    MAX(FirstName) AS FirstName,
    MAX(LastName)AS LastName,
    MAX(Main) AS Main,
    MAX(Department) AS Department
    FROM mytable
    GROUP BY CustID
    MERGE mytable AS Tar
    USING cte AS Src
    ON Tar.PKey = Src.PKey
    WHEN MATCHED THEN
    UPDATE SET Tar.CustID = Src.CustID, Tar.FirstName = Src.FirstName,Tar.LastName = Src.LastName, Tar.Main = Src.Main,Tar.Department = Src.Department
    WHEN NOT MATCHED BY SOURCE THEN
    DELETE
    SELECT * FROM MyTable
    DROP TABLE MyTable;
    If you do care about the Identity Pkey, as per the expected output, my understanding on your logic is like below.
    ;WITH cte AS
    SELECT PKey, CustID,V1,V2,V3,V4,ROW_NUMBER() OVER(PARTITION BY CustID ORDER BY v1+v2+v3+v4 DESC) AS RN
    FROM MyTable
    CROSS APPLY(SELECT CASE WHEN FirstName ='' THEN 0 ELSE 1 END AS v1) AS cat1
    CROSS APPLY(SELECT CASE WHEN LastName ='' THEN 0 ELSE 1 END AS v2) AS cat2
    CROSS APPLY(SELECT CASE WHEN Main ='' THEN 0 ELSE 1 END AS v3) AS cat3
    CROSS APPLY(SELECT CASE WHEN Department ='' THEN 0 ELSE 1 END AS v4) AS cat4
    ,cte2 AS
    SELECT DISTINCT
    CustID,
    MAX(FirstName) AS FirstName,
    MAX(LastName)AS LastName,
    MAX(Main) AS Main,
    MAX(Department) AS Department
    FROM mytable
    GROUP BY CustID
    ,cte3 AS
    SELECT c2.CustID,c2.FirstName,c2.LastName,c2.Main,c2.Department,c.PKey FROM cte2 c2 JOIN cte c ON c.CustID = c2.CustID WHERE NOT EXISTS(SELECT 1 FROM cte WHERE RN<c.RN)
    MERGE mytable AS Tar
    USING cte3 AS Src
    ON Tar.PKey = Src.PKey
    WHEN MATCHED THEN
    UPDATE SET Tar.CustID = Src.CustID, Tar.FirstName = Src.FirstName,Tar.LastName = Src.LastName, Tar.Main = Src.Main,Tar.Department = Src.Department
    WHEN NOT MATCHED BY SOURCE THEN
    DELETE
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • Cfloop query only finding one record

    I can't figure this out. My query is only finding one record
    (in certain instances), but there are more records in the table. It
    seems to the the problem when I use "LIKE". Everything prints fine
    if I use equals.
    Here's my code:
    <cfparam name="form.kword" default="">
    <cfparam name="form.fyear" default="2008">
    <cfparam name="form.fnum" default="">
    <cfparam name="form.titlew" default="">
    <cfparam name="form.doctrak" default="">
    <cfparam name="url.sterm" default="">
    <cfparam name="url.fyear" default="">
    <cfparam name="url.qtype" default="">
    <!---Queries--->
    <!---From FORM Query the forms table--->
    <cfquery name="displayformresults"
    datasource="#mydatasource#">
    SELECT fstrkeywords, fstrdoctrak, fstrformnum, fstryear,
    fstrtitle, fstrfilesize, fstrfiletype, fdtmrevdt, fblnfillin,
    fstraddendum
    FROM tblext_forms
    WHERE fstrkeywords LIKE '%#form.kword#%' AND fstryear =
    '#form.fyear#' AND fstrformnum LIKE '%#form.fnum#%' AND fstrtitle
    LIKE '%#form.titlew#%' AND fstrdoctrak LIKE '%#form.doctrak#%'
    ORDER BY fstrtitle, fstryear ASC
    </cfquery>
    <cfdump VAR="#displayformresults#">

    <!---Variable anyyeardisplay has been declared within a
    <cfif> tag so where the necessary condition is not met it
    means it would not been declared yet before you ask ColdFusion to
    output it on the table... trouble!
    So amend as below. If for any reason you need to put variable
    nicefdtmrevdt within a conditional statement as well, just do the
    same (as shown) --->
    <cfloop query="displayformresults">
    <cfif "#displayformresults.fstryear#" IS "">
    <cfset #displayformsesults.fstryear# = "2008">
    <cfelse>
    <cfset anyyeardisplay = #displayformresults.fstryear#>
    </cfif>
    <cfset nicefdtmrevdt =
    #DateFormat(#displayformresults.fdtmrevdt#, "mm-dd-yyyy")#>
    <cfoutput>
    <tr>
    <td
    class="fifteen">#displayformresults.fstrformnum#</td>
    <td class="fifteen"><cfif isDefined
    ("anyyeardisplay")>
    #anyyeardisplay#
    </cfif></td>
    <td class="fortyfive"><a href=""
    title="#displayformresults.fstrdoctrak#">#displayformresults.fstrtitle#</a></td>
    <td class="twentyfive"><cfif isDefined
    ("nicefdtmrevdt")>
    #nicefdtmrevdt#
    </cfif>
    <br />
    (#displayformresults.fstrfiletype#,
    #displayformresults.fstrfilesize#)</td>
    </tr>
    </cfoutput>

  • Query to find duplicate datas

    Hi,
    can any one help me the query to find the duplicate data from a column.

    maybe this example might be of some help.
    SQL> select * from employees;
    YEAR EM NAME       PO
    2001 04 Sarah      02
    2001 05 Susie      06
    2001 02 Scott      91
    2001 02 Scott      01
    2001 02 Scott      07
    2001 03 Tom        81
    2001 03 Tom        84
    2001 03 Tom        87
    8 rows selected.
    SQL> -- based on the output above we know that there is duplicates on scott and tom
    SQL> -- now we need to identified how many are duplicates by grouping into year, empcode, and name
    SQL> select year, empcode, name, position,
      2         row_number() over (partition by year, empcode, name
      3                            order by year, empcode, name, position) as rn,
      4         count(*) over (partition by year, empcode, name) as cnt
      5   from employees;
    YEAR EM NAME       PO         RN        CNT
    2001 02 Scott      01          1          3
    2001 02 Scott      07          2          3
    2001 02 Scott      91          3          3
    2001 03 Tom        81          1          3
    2001 03 Tom        84          2          3
    2001 03 Tom        87          3          3
    2001 04 Sarah      02          1          1
    2001 05 Susie      06          1          1
    8 rows selected.
    SQL> -- we have identified the duplicates on the above outputs by the counts
    SQL> -- now we want to query only rows that has duplicates
    SQL> select emp.year, emp.empcode, emp.name, emp.position, emp.cnt
      2    from (select year, empcode, name, position,
      3                 row_number() over (partition by year, empcode, name
      4                                    order by year, empcode, name, position) as rn,
      5                 count(*) over (partition by year, empcode, name) as cnt
      6            from employees) emp
      7   where rn = 1
      8     and cnt > 1;
    YEAR EM NAME       PO        CNT
    2001 02 Scott      01          3
    2001 03 Tom        81          3
    SQL>

  • Find duplicate records withouyqusing group by and having

    I know i can delete duplicate records using an analytic function. I don't want to delete the records, I want to look at the data to try to understand why I have duplicates. I am looking at tables that don't have unique constraints (I can't do anything about it). I have some very large tables. so I am trying to find a faster way to do this.
    for example
    myTable
    col1 number,
    col2 number,
    col3 number,
    col4 number,
    col5 number)
    My key column is col1 and col2 (it is not enforced in the database). So I want to get all the records that have duplicates on these fields and put them in a table to review. This is a standard way to do it, but it requires 2 full table scans of very large tables (many, many gigabtytes. one is 150 gbs and not partitioned), a sort, and a hash join. Even if i increase sort_area_size and hash_area_size, it takes a long time to run..
    create table mydup
    as
    select b.*
    from (select col1,col2,count(*)
    from myTable
    group by col1, col2
    having count(*) > 1) a,
    myTable b
    where a.col1 = b.col1
    and a.col2 = b.col2
    I think there is a way to do this without a join by using rank, dense_rank, or row_number or some other way. When I google this all I get is how to "delete them". I want to analyze them a nd not delete them.

    create table mytable (col1 number,col2 number,col3 number,col4 number,col5 number);
    insert into mytable values (1,2,3,4,5);
    insert into mytable values (2,2,3,4,5);
    insert into mytable values (3,2,3,4,5);
    insert into mytable values (2,2,3,4,5);
    insert into mytable values (1,2,3,4,5);
    SQL> ed
    Wrote file afiedt.buf
      1  select * from mytable
      2   where rowid in
      3  (select rid
      4      from
      5     (select rowid rid,
      6              row_number() over
      7              (partition by
      8                   col1,col2
      9               order by rowid) rn
    10          from mytable
    11      )
    12    where rn <> 1
    13* )
    SQL> /
          COL1       COL2       COL3       COL4       COL5
             1          2          3          4          5
             2          2          3          4          5
    SQL>Regards
    Girish Sharma

  • Reg: Find Duplicate Records and select max number of record in Table

    Hi Guys,
         This is Nagendra, India.
    my table structure is
    id  name  tempid  temptime
    1  xxx     123        date
    1 yyy       128       date
    1 sdd     173       date
    14 ree    184      date
    14 fded   189     date
    This is Table Structure, totally 15000+ records is there.
    My Requirement is showing id and max(tempId) value.
    id  name  tempid  temptime
    1   sdd    173      date
    14  fded   189     date
    Like that, I want to show all record(after hiding duplicate values ) like that Could you please solve this issue.
    With Regards,
    Nagendra

    ; WITH numbering AS (
        SELECT id, name, tempid, temptime,
               rowno = row_number() OVER(PARTITION BY id ORDER BY tempid DESC)
        FROM  tbl
    SELECT id, name, tempid, temptime
    FROM   numbering
    WHERE  rowno = 1
    The WITH thing defines a Common Table Expression which is a locally defined view which only exists for this query. The row_number function numbers the rows, re-starting on 1 for every id, and they are numbering in falling tempid order. Thus, by
    selecting all rows from the CTE with rono = 1, we get the row with the highest tempid for each id.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Stuck on sql query to find parent records that have the same child records

    Oracle 10gR2 Enterprise Edition.
    Hi,
    I'm trying to write some logic to look for records in a parent table, which have the exact same values in a child table.
    This is part of a bigger query, but I'm stuck on this part for now, so I've mocked up some simplified tables below to capture the core of the
    problem I'm stuck on.
    Let say I've got a parent table Manager, a child table Employee and there's a many to many relationship between them.
    The aptly named Join_Table handles the relationship between them. So one manager can manage many employees, one employee can be managed by
    many managers.
    I've a feeling this is stupidly easy, but I seem to be suffering from a bad bout of brain freeze today!
    -- parent table
    CREATE TABLE manager (
    id      number primary key,
    name      varchar2(100));
    -- child table
    CREATE TABLE employee (
    id          number primary key,
    name      varchar2(100));
    -- link table
    CREATE TABLE join_table (
    manager_id          NUMBER,
    employee_id      NUMBER,
    CONSTRAINT join_table_pk PRIMARY KEY (manager_id, employee_id),
    CONSTRAINT manager_fk FOREIGN KEY (manager_id) REFERENCES manager(id),
    CONSTRAINT employee_fk FOREIGN KEY (employee_id) REFERENCES employee(id)
    -- Insert some managers
    INSERT INTO manager (id, name) VALUES (1, 'John');
    INSERT INTO manager (id, name) VALUES (2, 'Bob');
    INSERT INTO manager (id, name) VALUES (3, 'Mary');
    INSERT INTO manager (id, name) VALUES (4, 'Sue');
    INSERT INTO manager (id, name) VALUES (5, 'Alan');
    INSERT INTO manager (id, name) VALUES (6, 'Mike');
    -- Insert some employees
    INSERT INTO employee (id, name) VALUES (101, 'Paul');
    INSERT INTO employee (id, name) VALUES (102, 'Simon');
    INSERT INTO employee (id, name) VALUES (103, 'Ken');
    INSERT INTO employee (id, name) VALUES (104, 'Kevin');
    INSERT INTO employee (id, name) VALUES (105, 'Jack');
    INSERT INTO employee (id, name) VALUES (106, 'Jennifer');
    INSERT INTO employee (id, name) VALUES (107, 'Tim');
    -- Insert the links
    -- John manages Paul, Simon, Ken
    INSERT INTO join_table (manager_id, employee_id) VALUES (1, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (1, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (1, 103);
    -- Bob manages Paul, Simon, Kevin, Jack
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 104);
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 105);
    -- Mary manages Jennifer, Tim
    INSERT INTO join_table (manager_id, employee_id) VALUES (3, 106);
    INSERT INTO join_table (manager_id, employee_id) VALUES (3, 107);
    -- Sue manages Jennifer, Tim
    INSERT INTO join_table (manager_id, employee_id) VALUES (4, 106);
    INSERT INTO join_table (manager_id, employee_id) VALUES (4, 107);
    -- Alan manages Paul, Simon, Ken, Jennifer, Tim
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 103);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 106);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 107);
    -- Mike manages Paul, Simon, Ken
    INSERT INTO join_table (manager_id, employee_id) VALUES (6, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (6, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (6, 103);
    -- For sanity
    CREATE UNIQUE INDEX employee_name_uidx ON employee(name);So if I'm querying for manager John, I want to find the other managers who manage the exact same list of employees.
    Answer should be Mike.
    If I'm querying for manager Mary, answer should be Sue.
    This query will give me the list of managers who manage some of the same employees as John, but not the exact same employees...
    SELECT DISTINCT m.name AS manager
    FROM manager m, join_table jt, employee e
    WHERE m.id = jt.manager_id
    AND jt.employee_id = e.id
    AND e.id IN (
         SELECT e.id
         FROM manager m, join_table jt, employee e
         WHERE m.id = jt.manager_id
         AND jt.employee_id = e.id
         AND m.name = 'John')
    ORDER BY 1;I thought about using set operations to find managers whose list of employees minus my employees is null and where my employees minus their list of employees is null. But surely there's a simpler more elegant way.
    Any ideas?
    Btw, I need to run this as a batch job against tables with >20 million rows so query efficiency is key.

    What about...
    WITH manager_list AS
    SELECT name,
            LTRIM(MAX(SYS_CONNECT_BY_PATH(id,','))
            KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
    FROM   (SELECT m.name,
                    e.id,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev
             FROM   manager m,
                    join_table jt,
                    employee e
      WHERE m.id           = jt.manager_id
      AND   jt.employee_id = e.id
      AND   m.name = :P_MANAGER)
      GROUP BY name
      CONNECT BY prev = PRIOR curr AND name = PRIOR name
      START WITH curr = 1
    ), all_list AS
    SELECT name,
            LTRIM(MAX(SYS_CONNECT_BY_PATH(id,','))
            KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
    FROM   (SELECT m.name,
                    e.id,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev
             FROM   manager m,
                    join_table jt,
                    employee e
      WHERE m.id           = jt.manager_id
      AND   jt.employee_id = e.id)
      GROUP BY name
      CONNECT BY prev = PRIOR curr AND name = PRIOR name
      START WITH curr = 1
    SELECT a.*
    FROM   manager_list m,
           all_list a
    WHERE  m.employees = a.employeesWould be easier in 11g, but I don't have an installation here so this is based on 10g.
    Cheers
    Ben

  • Query to find duplicate sql executed

    What is the sql to find the duplicate sql executed and count of executions.
    I need the query ( though we can get directly the results from OEM)
    Please let me know.
    Thanks
    Naveen

    >
    What is the sql to find the duplicate sql executed and count of executions.
    I need the query ( though we can get directly the results from OEM)Get to know V$SQL, V$SQL_AREA and V$SQL_TEXT.
    Check out Christoper Lawson's Oracle performance tuning book - it's
    very good on the basics of this subject.
    HTH.
    Paul...
    Naveen--
    When asking database related questions, please give other posters
    some clues, like OS (with version), version of Oracle being used and DDL.
    Other trivia such as CPU, RAM + Disk configuration might also be useful.
    The exact text and/or number of error messages is useful (!= "it didn't work!"). Thanks.
    Furthermore, as a courtesy to those who spend time analysing and attempting to help,
    please do not top post and do try to trim your replies!

  • Need sql query to find ten records in sequential manner

    Plz help me
    I need to have ten records on my page. at the bottom of the page, i want to dispaly the number of pages that have records , just as a google page shows at the bottom. if the moves user moves to the next page , the next ten records must be displayed. suppose the user selects and clicks 5th page then the records on that page are displayed.How can i write the sql query corresponding to this specificationi don't want all records to be fetched at the same time and put on the server, rather whenever the page is opened , the corresponding records are fetched and displayed

    Hi
    Maybe you should use a CachedRowSet.
    Look this:
    http://developer.java.sun.com/developer/technicalArticles/javaserverpages/cachedrowset/
    This page has a example that will help you.

  • How do you run a query to view duplicate records?

    I need to run a query with the query generator to produce a list of matching records. We have a problem whereby sales orders can come in by phone and email and therefore they sometimes get put on the system twice. I need to produce a query which picks up any orders where the CardCode and Document Value match, within say two days of each other.
    Please help?

    Hi Wendy Burt....
    Try This
    SELECT   COUNT(T0.[CardCode])as Duplicatecount, T0.[CardName], T0.[DocTotal] FROM ORDR T0
    WHERE T0.[DocTotal] IN ( select t1.doctotal from ORDR t1
    where t0.cardcode=t1.CardCode AND datediff(dd, t0.docdate,getdate())<=2)
    AND
    T0.CARDCODE IN ( select t1.CardCode from ORDR t1
    where t0.cardcode=t1.CardCode AND datediff(dd, t0.docdate,getdate())<=2)
    and t0.docstatus = 'O' and t0.doctotal >0
    GROUP BY  T0.[CardName], T0.[DocTotal]
    Regards,
    Kennedy

  • Find duplicate records

    Hi,
    I have a 800,000-row table with unique ID. Recently we're given criteria for record duplication based on 7 out of 20 table's columns. We would like know which record ID has duplication value based on the exact match of those 7 columns.
    Example
    ID PRODUCT DEPT LEVEL
    ============================
    1 PRINTER ABC 1
    2 PRINTER ABC 2
    3 PRINTER DEF 2
    4 TOMER ABC 3
    If new criteria of duplication is based on the exact data match of column PRODUCT and DEPT, record ID 1 and 2 are considered duplicated.
    Is there any easy way to it on 800,000 row table?
    Thanks in advance
    -i

    here is some examples that might be of help.
    SQL> select * from employees;
    YEAR EM NAME       PO
    2001 02 Scott      91
    2001 02 Scott      01
    2001 02 Scott      07
    2001 03 Tom        81
    2001 03 Tom        84
    2001 03 Tom        87
    6 rows selected.
    SQL> select year, empcode, name, position,
      2         row_number() over (partition by year, empcode, name
      3                            order by year, empcode, name, position) as rn
      4    from employees;
    YEAR EM NAME       PO         RN
    2001 02 Scott      01          1
    2001 02 Scott      07          2
    2001 02 Scott      91          3
    2001 03 Tom        81          1
    2001 03 Tom        84          2
    2001 03 Tom        87          3
    6 rows selected.
    SQL> Select year, empcode, name, position
      2    From (Select year, empcode, name, position,
      3                 row_number() over (partition by year, empcode, name
      4                                    order by year, empcode, name, position) as rn
      5            From employees) emp
      6   Where rn = 1;
    YEAR EM NAME       PO
    2001 02 Scott      01
    2001 03 Tom        81
    SQL>

  • Query to find duplicate referance numbers

    Dear,
    We have somehow managed to create the same sales order twice or more. In the field "Customer Ref. No." we always type in BP's ref. no. So what I need is a query which will show me all open sales orders which have more than one identical "Customer Ref. No." (ORDR.NumAtCard)
    So what I got so far is:
    SELECT T0.[DocNum], T0.[NumAtCard] FROM ORDR T0 WHERE T0.[DocStatus] <> 'C' AND ????
    Hope anyone can help. Thanks.
    BR
    Kjetil Sandvik

    Hi Kjetil Sandvik,
    If you want to block Sales Order with duplicare Customer Ref. No then
    Try this SP.....
    if (@object_type = '17')
    and (@transaction_type= 'A' or @transaction_type= 'U')
    begin
    declare @Refno as varchar(100), @Cust as varchar(100)
    if @object_type = '17'
    begin
    select @Refno = NumAtCard,
    @Cust=CardCode
    from ordr
    where DocEntry =@list_of_cols_val_tab_del
    if 1 != (select count(DocEntry) from ordr with(nolock) where NumAtCard = @Refno and CardCode=@Cust)
    begin
    select @error = 1
    select @error_message = 'Customer Ref. No Should Not Be Repeated. ! '
    end
    end
    end
    Thanks,
    Srujal Patel

Maybe you are looking for