使用ms-sql 2008 r2;我相信这非常简单。我正在尝试确定哪个唯一值{ISIN}已链接到多个标识符。输出示例如下:
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 :(得分: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都有一行是一个错误但是isin-3,比如说,链接到两个separe行中的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 BY子句。如果这是投入生产,我会建议。或者如果你的桌子有五亿行。如果您只是需要进行一些分析,我希望能满足要求。