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

Similar Messages

  • I accidentally ended up with two versions of Photoshop Elements in my cart and can't find a way to delete one.  Also, I am living in Peru and want to purchase the US version.  Will it still download?

    I accidentally ended up with two versions of Photoshop Elements in my cart and can't find a way to delete one.  Also, I am living in Peru and want to purchase the US version.  Will it still download?

    Karenl33987234 I am sorry that you are facing difficulty contacting our support team. You should be able to chat with a member of our support team at Contact Customer Care.  If this link does not work then I would recommend using a different web browser, smart phone, tablet, or computer to visit the page.
    I did review the account which you utilized to post to this public forum but I did not see any orders or Adobe software titles registered.  Please make sure to sign in with the Adobe ID/e-mail address tied to the order you are facing difficulties with.

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

  • 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 and Update

    hi,
    Can anyone please provide me a query to find out names having " , " commas like servi,ce actually I have a service name column in a table where i need to find names with commas "," and remove by update.
    I appreciate, if anyone provide me query to find those services having commas in between and a separate update statement to remove them
    Thanks in advance for your cooperation
    Regards,
    ahon

    hello
    used the sql command REPLACE & INSTR function
    example :
    Query :
    select service_name
    from YOURTABLE
    where instr(service_name,',') > 0 -- This return those with ',' char
    to update :
    update YOURTABLE
    set service_name = replace(service_name,',','') -- to replace the ',' char to null values
    where instr(service_name,',') > 0
    But before doing that back up your table or do it in a copy of that..
    Syntax
    REPLACE ( string_expression , string_pattern , string_replacement )
    Arguments
    string_expression
    Is the string expression to be searched. string_expression can be of a character or binary data type.
    string_pattern
    Is the substring to be found. string_pattern can be of a character or binary data type. string_pattern cannot be an empty string (''), and must not exceed the maximum number of bytes that fits on a page.
    string_replacement
    Is the replacement string. string_replacement can be of a character or binary data type.
    charles.
    if this find helpful or correct then mark it accordingly

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

  • 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

  • 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

  • Query to find Last updated..

    Hi all,
    If I have a table with accounts, users and last_on_date, how can I find the user who accessed an account most recently.
    Say, the table is as follows:
    a/c user c_date
    a1 u1 03/04/2007
    a1 u2 03/05/2007
    a2 u3 03/05/2007
    a2 u1 03/07/2007
    a2 u2 03/06/2007
    a3 u3 04/05/2007
    What query would fetch the following:
    a/c user c_date
    a1 u2 03/05/2007
    a2 u1 03/07/2007
    a3 u3 04/05/2007
    Any help is appreciated.

    That's only part of the OP's Q.
    The OP asked for the most recent user of a particular account not the most recent access of each user to each account.
    so this would do the trick:
    SELECT AC,
           USER,
           C_DATE
    FROM (SELECT AC,
                 USER,
                 C_DATE,
                 ROW_NUMBER() OVER (PARTITION BY AC ORDER BY C_DATE DESC) RN
            FROM YOUR_TABLE)
    WHERE RN = 1;

  • Updating and deleting multiple rows

    Hi!
    I have a form on a table with report page where I can filter data through columns.
    1. Is it possible to create a button that will delete all the filtered data?
    2. Also, I would like to be able to update any column of the filtered dataset to a certain value (that needs to be input somehow). Is that possible?
    So far I can only update or delete one row at a time, which isn't useful as I sometimes need to change 100 rows at a time with the same value or delete them.
    When I use tabular form, I can't filter rows, but I can delete multiple rows...
    Also if there are similar examples, could you please send me a link; I can't seem to find any.
    I'm using Apex 4.2.2.
    Best Regards,
    Ivan

    Deleting multiple rows - [url https://forums.oracle.com/forums/thread.jspa?threadID=2159983]common question
    Best answered with Martin's example
    http://www.talkapex.com/2009/01/apex-report-with-checkboxes-advanced.html
    Depends how you filter your data. You could identify all your limiting variables, and reverse the where clause in a delete process.
    As for point 2, you can define a button that redirects to the page, and you can defined all the item values you like using the Action link.
    There is likely an example in the supplied package applications.
    Scott

  • Query to find package creation, modify

    Hi,
    Does anybody have the query to find the package Creation, Deletion and modification record?
    I do not want the Audit or Staus messages to check.
    Need a proper report on Packages created, modified etc..
    System Security analyst at CapG

    Hi,
    Does anybody have the query to find the package Creation, Deletion and modification record?
    I do not want the Audit or Staus messages to check.
    Need a proper report on Packages created, modified etc..
    System Security analyst at CapG
    Any updates?? Atleaset few suggestions..
    System Security analyst at CapG
    Ok, look at the status message. They will be exactly the same as any query and the status message query already exist. Why re-event the wheel?
    Garth Jones | My blogs: Enhansoft and
    Old Blog site | Twitter:
    @GarthMJ

  • Finding duplicate values in a column with different values in a different c

    I'm finding duplicate values in a column of my table:
    select ba.CLAIM_NUMBER from bsi_auto_vw ba
    group by ba.CLAIM_NUMBER
    having count(*) > 1;
    How can I modify this query to find duplicate values in that column where I DON'T have duplicate values in a different specific column in that table (the column CLMT_NO in my case can't have duplicates.)?

    Well, you can use analytics assuming you don't mind full scanning the table.....
    select
       count(owner) over (partition by object_type),    
       count(object_type) over (partition by owner)
    from all_objects;You just need to intelligently (i didn't here) find your "window" (partition clause) to sort the data over, then make use of that (the analytics would be in a nested SQL and you'd then evaluate it in outside).
    Should be applicable if i understand what you're after.
    If you post some sample data we can mock up a SQL statement for you (if what i tried to convey wasn't understandable).

  • Making sure a row was inserted, updated, or deleted

    What is the best way to make sure a Coldfusion query run ?
    For updates and deletes, what I am starting to do is use result in cfquery and make sure result name dot recordcount is 1.
    For inserts, I'm checking to see if result name dot identitycol is there.
    Are those good ways to do make sure queries ran ?

    Added to that we can also use cftry and cfcatch tag to handle the error if anything wrong occurs at the runtime. If we are having any interdependent statement like insert than update and if update failed than we should roll back the insertion then we can go for cftransaction tag.

  • After 10.4.9 Update I have duplicate mailboxes! Deleted one and lost mail!

    Hello,
    Mail is giving me problems after the 10.4.9 update. I have duplicate mailboxes for each of my four accounts. After deleting the duplicate, I have lost all mail in that mailbox. It seems that although the mailboxes appear to be duplicates, they are not and deleting one deletes contents in the other. I'd like to know whether there is any way I could get back the mail I lost. Thank you in advance to those of you who could offer suggestions.

    You’re welcome.
    Yes, solving this problem will probably require re-creating com.apple.mail.plist. I’d like to know what the situation really is before proceeding, though.
    How many accounts do you see in Mail > Preferences > Accounts and what type are they (POP, IMAP, .Mac)?
    Report the names of the files and folders present at the first level of the ~/Library/Mail/ folder in the Finder.
    In order to post the exact file names and avoid typing mistakes, proceed as follows. In the Finder, go to ~/Library/Mail/. With that folder open, if you’re looking at it in View > as List mode, first collapse any expanded folders so that the Finder only shows the files and folders at the first level of ~/Library/Mail/. Now, do Edit > Select All (⌘A), then Edit > Copy (⌘C), and paste it in your reply to this post.
    Before actually posting that information, edit it so that the file/folder names do not reveal any details you should keep private, e.g. replace any real username with “username” to hide your real email address. Try to be consistent in how you disguise those details, though, as we may need to refer to them in subsequent posts.
    Note: For those not familiarized with the ~/ notation, it refers to the user’s home folder, i.e. ~/Library is the Library folder within the user’s home folder.

  • How can I find duplicate photos without going through all 5000  one at a time?

    How can I find duplicate photos withough going through all 5000 one by one?  I have numerous duplications and used to be able to click on Edit and Find Duplicates and could clean up all the duplicates easily.  Now that option is gone.  I still have lots to delete.

    I do backup everything using Carbonite.Please don't tell me it is bad, too :-)  Also, especially with photo files, I do have other backups of those I am concerned about. Probably not the best in the world, but a log better than many people who I know about.
    Again, I really have not seen any problems with MacKeeper.  I imagine that if people do not read and follow instructions, they could have problems. For example, it is possible to scan all your files and see them in order of size. If you start deleting things, without knowing what you're deleting, you could cause trouble. Even when the computer scans for so-called junk files, you can look at them and decide for yourself if they are truly junk. I've never seen any that were not.
    So, I will look at the complaints anyway to see them. I am open to change, although I hope I do not need to drop Mackeeper as it does things like keeping tracking of applications updates that I need. Please note that I definitely am not a MacKeeper employee or anything close. Just using it and happy with the the results.
    I did download the recommend file for finding photo dupes and ran it.  There were very few duplicates, in any case, As I said before, the MacKeeper dupe finder was very slow when I ran it over ALL my files. That was a year ago and it did find many non-text dupes.

Maybe you are looking for

  • List items in JSF 1.2?

    Hello, I'm in the process of learning JSF (I have a little ASP.NET background). I'd like to be able to iterate through a List of items and render them as buttons on the jsp page, inside list items in an unordered list: <ul>   <li><button name="btn1"

  • Time machine and wiki data recovery

    I'm running into an issue where I need to recover wiki data from a Time Machine back-up.. The backstory: The wiki server was running on a 2008 Mac Pro which had an unrecoverable disk failure.  Given the age of the machine, the power it drew, and the

  • How to Enhance SPRO so that Config can be maintained in Production System

    I have a requirement to Enhance the configuration nodes within SPRO so that the Client can directly edit specific configurations that would pose a risk to the business if they have to wait for the production release. I have to enhance the following N

  • Batch PDF Reports raising error REP-3002: Error initializing printer.

    We are running Reports 6.0 in Windows NT 4.0. When run manually the PDF reports are able to see the PostScript Print Driver and generates PDF output. We have a batch job on the NT that gets kicked off from the Mainframe via XPATH, which spawns multip

  • Best Practice Scorecarding

    I have downloaded XAPPFINBPSC00_0-10004332.sca from service market place under swdc -> Entry by Application Group -> SAP xApps.  Our basis team had installed the .sca file using SDM.  I have SAP_ALL kind of authorizations on poratl.  But I still do n