1

i have column and rows in my table as below

col1  col2  col3  col4
-----------------------
A     1     100   AA
B     2     200   BB
B     1     100   AA
A     2     200   BB

col2, col3 and col4 have duplicate rows, but not in col1. i want the final result is

col1  col2  col3  col4
-----------------------
A     1     100   AA
B     2     200   BB

i've tried this queries, but not Working

DELETE aliasName FROM 
(
    SELECT *, ROW_NUMBER() OVER (
      PARTITION BY col1,col2,col3,col4 ORDER BY col1
    ) AS rowNumber FROM Table) aliasName
WHERE rowNumber > 1

if i remove the PARTITION BY col1, the result is

col1  col2  col3  col4
-----------------------
A     1     100   AA
A     2     200   BB

this my real table result is

JSP/14/06/2017/1810759183   SRK/14/06/2017/1265996087   JS_43345.43556  BB  20000   1
JSP/14/06/2017/800205939    SRK/14/06/2017/2089009939   JS_43345.43556  BB  20000   1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
FannyKaunang
  • 91
  • 1
  • 8
  • 1
    How do you know which value in `col1` to keep? In your sample data, why are you retaining the first and second rows instead of the third and fourth rows? – iamdave Jun 14 '17 at 10:28
  • that data is made by looping in my program, there are 3 times looping before it send to database – FannyKaunang Jun 14 '17 at 10:35
  • That doesn't answer my question. All 4 rows of data in your question are unique, which means you are actively excluding the 3rd and 4th rows without explaining why. What criteria is used to pick the first 2 rows to keep instead of the second 2 rows? – iamdave Jun 14 '17 at 10:38
  • yeah sir, i have i more column is unique that not i including here. total column is 5th..i just need rows of the col1 to keep col2,col3 and col4 together, but not have duplicates in the col1. i hope you understand what i am saying sir – FannyKaunang Jun 14 '17 at 10:45
  • What you are asking for makes no sense. **All 4 rows in your question are unique** regardless of the 5th column you have just mentioned. – iamdave Jun 14 '17 at 10:49
  • can you help me sir? just skip the criteria that makes you confused, i just need that col1 have no duplicate rows sir – FannyKaunang Jun 14 '17 at 11:05
  • try this https://stackoverflow.com/questions/985384/delete-duplicate-records-from-a-sql-table-without-a-primary-key – asmgx Jun 15 '17 at 01:58

1 Answers1

0

try this...

DELETE aliasName FROM 
(
    SELECT *, ROW_NUMBER() OVER (
       ORDER BY col1
    ) AS rowNumber FROM inp) aliasName
WHERE rowNumber%2 = 0
Nithin
  • 1,376
  • 12
  • 29