该表具有唯一ID的单行,重复行和具有相同ID的多个不同行。
我需要获取具有相同ID
ID Major Major_ID
1234 chemi 1111
1234 chemi 1111
673 biology 2222
673 phylo 3333
2480 comput 4444
2480 busine 5555
243 accout 6666
492 finance 7777
4812 engine 8888
带有示例数据的SQLFiddle:http://www.sqlfiddle.com/
预期结果:
ID Majoy Major_ID
673 biology 2222
673 phylo 3333
2480 comput 4444
2480 busine 5555
答案 0 :(得分:1)
CREATE TABLE NEW_TABLE AS
SELECT ID,Majoy,Major_ID
FROM YOUR_TABLE T
WHERE NOT EXISTS (
select null
From YOUR_TABLE
WHERE T.ID=ID AND T.Majoy = Majoy and T.Major_ID = Major_ID
GROUP BY ID,Majoy,Major_ID
HAVING COUNT(*)>1
)
子查询获取重复数据。然后我用主要查询排除结果集的这些行。 create table as语句根据查询中的数据创建一个新表,如在sql server中选择进入
答案 1 :(得分:0)
Select Id, Major, Major_ID
from First_Table
Minus
Select Id, Major, Major_ID
from Second_Table
答案 2 :(得分:0)
您可以使用COUNT作为分析函数来计算每个id的不同专业,然后只选择那些计数超过1的专业。
SELECT id, major, major_id
FROM (SELECT id,
major,
major_id,
COUNT (DISTINCT major) OVER (PARTITION BY id) AS count_
FROM first_table)
WHERE count_ > 1;