Tuesday 14 November 2017

SAP HANA: How to DELETE duplicate records from the Table but keep original record?


How to delete duplicate records from the Table but keep the original record?

Scenario:

DEPTID
DEPTNAME
PRICE
1
Blog
2000
2
Article
5000
3
Resource
7000
4
Book
500
4
Book
500
1
Blog
1000

From the above table, DEPTID 4 have some duplicate records. i.e., DEPTID, DEPTNAME, PRICE columns having the same value.

Solution:

Code:

delete from "ADZSUPRI_PRACTICE"."cpy_tab_Department"
where "$rowid$" in
(
SELECT   LEAD("$rowid$") over (partition by DEPTID,DEPTNAME,"price" order by DEPTID,DEPTNAME)
from "ADZSUPRI_PRACTICE"."cpy_tab_Department" ;
)

Result:

DEPTID
DEPTNAME
PRICE
1
Blog
2000
2
Article
5000
3
Resource
7000
4
Book
500
1
Blog
1000

 Note:
When we execute the inner query i.e., Sub query we get the result like as shown below:

Code:

SELECT   LEAD("$rowid$") over (partition by DEPTID,DEPTNAME,"price" order by DEPTID,DEPTNAME) from "ADZSUPRI_PRACTICE"."cpy_tab_Department" ;


ROWID
·         For each row in the database, the rowid pseudo column returns the address of the row.
·         Usually, a rowid value uniquely identifies a row in the database.
·         Rowid values have several important uses:
v  They are the fastest way to access a single row.
v  They can show you how the rows in a table are stored.
v  They are unique identifiers for rows in a table.

6 comments:

  1. Wow..ur blogging technique is unique as u gave the output as well with explanation...thank you for the sharing such knowledge..keep sharing..

    ReplyDelete
  2. Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
    sql server dba online training
    SQL dba online course

    ReplyDelete
  3. Thank you Kabil,very useful blogs.

    "$rowid$" is not supported in SAP HANA 1.0 SPS 10.Any other alternative to delete duplicates?

    ReplyDelete
  4. After reading so many queries at other portal, I found only this to be working out. Thanks.

    ReplyDelete
  5. How To delete duplicate data from query generator?

    ReplyDelete