-1

The table has single row with unique ID, duplicated rows and multiple different rows with the same ID.
I need to pick up those rows which have different values with the same 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 with sample data: http://www.sqlfiddle.com/

The expected result:

ID    Majoy    Major_ID
673    biology   2222
673    phylo     3333
2480     comput    4444
2480     busine    5555
4

3 回答 3

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 中的 select into)创建一个新表

于 2013-10-11T15:12:11.063 回答
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;
于 2013-10-11T16:57:47.520 回答
0
Select Id, Major, Major_ID
from First_Table
Minus
Select Id, Major, Major_ID
from Second_Table
于 2013-10-11T15:37:33.723 回答