SQL比较并确定重复和&同一个表中的非重复记录

时间:2011-03-31 21:44:07

标签: sql sql-server-2005 duplicates

我有下表示例数据

Customer     Location     ID    Attribute1     Attribute2
  Cust1        Loc1       1        High          None
  Cust1        Loc1       2        High          None
  Cust1        Loc1       3        Low           None 

根据上面的示例数据,前两个记录是重复的,最后一个是非重复的。所以,我需要创建两个表,一个用于非重复记录,一个用于重复。没有。这里显示的属性列只是示例,通常是大约10列。

表一

Customer     Location     ID    Attribute1     Attribute2
  Cust1        Loc1       1        High          None

表二

Customer     Location     ID    Attribute1     Attribute2
  Cust1        Loc1       3        Low           None

这可以在一个SQL查询中执行吗?

感谢您的任何建议, Javid

1 个答案:

答案 0 :(得分:0)

你可以很容易地做到这一点......

(我在oracle数据库中运行下面的查询我不确定abour sql server但是在sql语句中它正在做正确的操作)

insert all
when (Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2) in 
     (select Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2 
         from base_table 
         group by Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2 
         having count(*)>1) then 
   into Table_One (Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2)
      values (Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2)
else
   into table_two (Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2 )
       value (Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2 ) 
select distinct Customer ,    Location ,    ID ,   Attribute1 ,    Attribute2 from base_table 

当您将所有列值重复时,此查询很有用...

但在你的问题中你所提供的数据是你没有重复的ROWS ..

查看ID列值..

如果您想检查相同的特定列组,请告诉此处..

相关问题