2014-04-07 32 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不同的地方我想分離出現的地方。我對上面的代碼做了輕微的修改。 –