How to remove duplicate rows in SQL

痞子三分冷 提交于 2019-12-12 02:58:09

问题


I want to remove duplicate rows in the following data.

Data:

a       a
a       b
a       c
a       d
a       e
b       a
b       b
b       c
b       d
b       e
c       a
c       b
c       c
c       d
c       e
d       a
d       b
d       c
d       d
d       e
e       a
e       b
e       c
e       d
e       e

The output should be column1 and column2 different. and it should not be the following data.

a       b
a       c
a       d
a       e
b       a
b       c
b       d
b       e
c       a
c       b
c       d
c       e
d       a
d       b
d       c
d       e
e       a
e       b
e       c
e       d

because a in column 1 and b in column2 is same as b in column1 and a in column2 or else simply i mean to say 1+2 is same as 2+1.

So, the output should be

a,b
a,c
a,d
a,e
b,c
b,d
b,e
c,d
c,e
d,e

回答1:


select distinct least(column_1, column_2), greatest(column_1, column_2)
from the_table
where column_1 <> column_2;



回答2:


This should give you what you need with a bit of reshuffle:

SELECT DISTINCT LEAST(column1, column2) as column1, GREATEST(column1, column2) as column2
FROM myTable

If you want to keep values in columns they belonged originally, try this:

WITH cte AS 
( SELECT t.*, ROW_NUMBER() OVER () RN
  FROM   myTable t)
SELECT * FROM cte t
WHERE NOT EXISTS (
   SELECT * FROM cte 
   WHERE 
      t.column1 IN (column1, column2) AND 
      t.column2 IN (column1, column2) AND
      t.RN > RN
  )


来源:https://stackoverflow.com/questions/25802219/how-to-remove-duplicate-rows-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!