GeekInterview.com
Series: Subject: Topic:
Question: 26 of 214

Remove redundant rows

how to remove redundant rows without any distinct columns in the table itself ?
Asked by: Anurag Shahi | Member Since Jul-2011 | Asked on: Jul 22nd, 2011

View all questions by Anurag Shahi

Showing Answers 1 - 5 of 5 Answers
kalavani

Answered On : Jul 25th, 2011

redundant means repeatedly.so we take one time others negligible.because redundant affect the answer part

  
Login to rate this answer.
AnithaLetchumi

Answered On : Jul 29th, 2011

View all answers by AnithaLetchumi

With the usage of rowid or ranking we can achieve deleting redundant rows in the table having no key or any distinct columns.

Code
  1. CREATE TABLE dup(n1 int,val1 VARCHAR2(10));
  2.  
  3. INSERT INTO dup VALUES ( 1, 'x');
  4. INSERT INTO dup VALUES ( 1, 'y');
  5. INSERT INTO dup VALUES ( 1, 'x');
  6. INSERT INTO dup VALUES ( 1, 'y');
  7. INSERT INTO dup VALUES ( 1, 'z');
  8. INSERT INTO dup VALUES ( 1, 'y');
  9. INSERT INTO dup VALUES ( 2, 'xx');
  10. INSERT INTO dup VALUES ( 2, 'xx');
  11. INSERT INTO dup VALUES ( 2, 'zz');
  12. INSERT INTO dup VALUES ( 2, 'zz');
  13.  
  14. SELECT * FROM dup;
  15.  
  16. DELETE FROM dup d1 WHERE d1.ROWID >
  17. ANY (SELECT d2.ROWID FROM dup d2 WHERE d1.n1=d2.n1 AND d1.val1=d2.val1);

  
Login to rate this answer.
dinesh.smhdr

Answered On : Sep 13th, 2011

View all answers by dinesh.smhdr

delete from emp where rowid not in (select min(rowid) from emp group by empno);

Here all the records having max(rowid) will be deleted and records with min(rowid) will be left behind giving unique records.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Allan Hunter

Answered On : Sep 21st, 2011

View all answers by Allan Hunter

You're question is vague, but from what I understand it to mean is to remove duplicate rows wihle keeping the columns in tact. The easiest way I know is to make your query based on what makes the data unique, and make your query with that in mind. For example, in table Emp, you have employee and in table payroll you have you have their work schedule and their salary information. You want to know the employee's name, their title,their weekly hours and gross pay. But, because there's a one-to-many correlation you have to find a way to make the data only appear once. I think this is how:

SELECT DISTINCT EMP.NAME, EMP.TITLE, PAYROLL.WHOURS, PAYROLL.ANNSAL
FROM EMP.EMPNO = PAYROLL.EMPNO
ORDER BY EMP.NAME

  
Login to rate this answer.
smonroy

Answered On : Sep 27th, 2011

View all answers by smonroy

Hi my Friend this is the best way to you issue

DELETE FROM table_name where camp1 in (SELECT camp1 FROM table_name GROUP BY camp1 HAVING Count(camp1)>1)

Example:

DELETE FROM ubication where serial_number in
(SELECT serial_number FROM ubication
GROUP BY serial_number HAVING Count(serial_number)>1)

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.