根据列选择不同的行组合

时间:2018-03-29 05:37:05

标签: mysql sql ssms

我有一个表,其中author1和author2列具有镜像数据,如(a,b)和(b,a)。但我不希望数据是多余的,因此需要SQL查询来消除具有(b,a)if(a,b)已经存在的记录。

author1              author2                No of Publications
Anna Spagnolli       Luciano Gamberini           115
Luciano Gamberini    Anna Spagnolli              115
Anna Spagnolli       Giuseppe Riva                66
Giuseppe Riva        Anna Spagnolli               66
Giuseppe Riva        Luciano Gamberini            60
Luciano Gamberini    Giuseppe Riva                60
Achim Jung           Anna Spagnolli               33
Anna Spagnolli       Achim Jung                   33
Anna Spagnolli       John A. Waterworth           33
John A. Waterworth   Anna Spagnolli               33

例如,我不想要第二条记录,因为它已经存在于第一条记录中

4 个答案:

答案 0 :(得分:2)

解决您的问题:

SELECT table1.author1, table1.author2, table1.No_Of_Publications FROM table1
LEFT JOIN table1 as tbl
ON table1.author1 = tbl.author2
AND table1.author2 = tbl.author1
WHERE table1.author1 < tbl.author1
OR tbl.author1 IS NULL

对于演示:

  

http://sqlfiddle.com/#!9/2263fa/14

答案 1 :(得分:1)

请尝试使用以下查询: -

select (case when author1> author2 then author2 else author1 end) as author1
      ,(case when author1> author2 then author1 else author2 end) as author2
      ,publications 
from calc
group by (case when author1> author2 then author2 else author1 end)
,(case when author1> author2 then author1 else author2 end)
,publications

答案 2 :(得分:0)

试试这个https://www.db-fiddle.com/f/buQqnt1YsfoWCuJoMdgVrz/0

CREATE TABLE table1 (
  author1 VARCHAR(100),
  author2 VARCHAR(100),
  publication INT
);

插入table1值('a','b',100);

插入table1值('b','a',100);

select   (case
  when t.author1>t.author2 then t.author1 else t.author2  END ) as a1 ,
   (case
  when t.author1>t.author2 then t.author2 else t.author1  END ) as a2 , publication

 from table1 as t group by a1, a2, publication;

答案 3 :(得分:0)

CREATE TABLE #TEMI1(AUTHOR1 CHAR(3),AUTHOR2 CHAR(3),NO_OF_PUB INT)

INSERT INTO #TEMI1 VALUES
('A','B',115),('B','A',115),('A','C',66),('C','A',66),('D','A',78),('B','C',115)

WITH CTE1 AS(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY NO_OF_PUB ORDER BY NO_OF_PUB) RNUM FROM #TEMI1 )

--SELECT * FROM CTE1 C1
--JOIN CTE1 C2
--ON C1.NO_OF_PUB=C2.NO_OF_PUB AND C1.AUTHOR1=C2.AUTHOR2 AND C1.AUTHOR2=C2.AUTHOR1


DELETE C1 FROM CTE1 C1 
JOIN CTE1 C2
ON C1.NO_OF_PUB=C2.NO_OF_PUB AND C1.AUTHOR1=C2.AUTHOR2 AND C1.AUTHOR2=C2.AUTHOR1
WHERE C1.RNUM>1