SQL删除列和第二列中的重复项-A,B和B,A

时间:2020-04-08 19:10:35

标签: sql

Column1  | Column2 
-------------------
     A   |  B
     B   |  A 

我要删除第二对B,A

4 个答案:

答案 0 :(得分:1)

此查询使用标准SQL:

select distinct t.column1, t.column2 
from tablename t
where t.column1 <= t.column2
or not exists (
  select 1 from tablename
  where column1 = t.column2 and column2 = t.column1
)

查看简化的demo

答案 1 :(得分:0)

大多数DBMS支持minimum()和great()函数。如果您有,则可以使用:

select distinct least(col1, col2) as col1, greatest(col1, col2)
from the_table;

答案 2 :(得分:0)

with data(c1, c2) as (
    select a, b from T union
    select b, a from T
), rnk as (
    select c1, c2,
        row_number() over (
            partition by c1, c2, abs(c1 - c2)
            order by sign(c1 - c2)) as rn
    from data
select c1, c2 from rnk where rn = 1;

这种方法可能是可扩展的,否则就此而言只是学术上的。不知道您是否实际上想做三个列的六个排列或四个列的24个排列等等,还是一个好主意。

答案 3 :(得分:0)

左抗联接方式:

module Main where

import Graphics.Gnuplot.Advanced
import Graphics.Gnuplot.Plot.TwoDimensional as P2D
import Graphics.Gnuplot.Graph.TwoDimensional as G2D
import Graphics.Gnuplot.Terminal.X11 as X11

zeroToTen = [0.0, 0.1 .. 10.0 :: Double]

main :: IO ()
main = do
  plot (X11.title "My Title" X11.cons) (P2D.function G2D.lines zeroToTen sin)
  _  <- getLine
  return ()