2014-04-07 21 views
0

使用ms-sql 2008 r2;我确信这非常简单。我试图确定一个唯一值{ISIN}与多个标识符相关联的位置。一个例子输出为:识别多个ID链接到唯一值的行

isin   entity_id 
XS0276697439 000BYT-E 
XS0276697439 000BYV-E 

这实际上是一个错误,我想寻找其他的情况下,人们可能不止一个ENTITY_ID链接到一个独特的ISIN。

这是我目前的工作,但它显然不正确:

select isin, entity_id from edm_security_entity_map 
where isin is not null 
--and isin = ('XS0276697439') 
group by isin, entity_id 
having COUNT(entity_id) > 1 
order by isin asc 

感谢您的帮助。

回答

0

埃利奥特,

我没有SQL的拷贝在我的面前,现在,所以如果我的道歉语法没有发现上。

我会通过寻找重复的开始:

select 
    x.isin 
    ,count(*) 
from edm_security_entity_map as x 
group by x.isin 
having count(*) > 1 

然后加入该回全表以找到那些重复来自:

;with DuplicateList as 
(
select 
    x.isin 
    --,count(*)  -- not used elsewhere 
from edm_security_entity_map as x 
group by x.isin 
having count(*) > 1 
) 
select 
    map.isin 
    ,map.entity_id 
from edm_security_entity_map as map 
inner join DuplicateList as dup 
    on dup.isin = map.isin; 

HTH, 迈克尔

所以你是说,如果isin-1对于entity-1和entity-2都有一行是一个错误,但是inin-3,比如链接到两个分隔行中的entity-3是好的?丑陋的,但可读的解决方案,即预先挂起另一CTE在前面的解决方案

;with UniqueValues as 
(select distinct 
    y.isin 
    ,y.entity_id 
from edm_security_entity_map as y 
) 
,DuplicateList as 
(
select 
    x.isin 
    --,count(*)  -- not used elsewhere 
from UniqueValues as x 
group by x.isin 
having count(*) > 1 
) 
select 
    map.isin 
    ,map.entity_id 
from edm_security_entity_map as map -- or from UniqueValues, depening on your objective. 
inner join DuplicateList as dup 
    on dup.isin = map.isin; 

,有具有附加GROUP更好的解决方案通过在最终的查询子句。如果这是进入生产,我会推荐。或者如果你的表有一个bajillion行。如果你只需要做一些分析,我希望以上就足够了。

+0

谢谢Michael;差不多了。这也返回多个实体ID是相同的链接到ISIN,这是正常的。这是实体ID不同的地方我想分离出现的地方。我对上面的代码做了轻微的修改。 –