我有一张包含材料信息的表格,其中一种材料有一种到多种成分。
表格如下:
material_id contstiuent_id constituent_wt_pct
1 1 10.5
1 2 89.5
2 1 10.5
2 5 15.5
2 7 74
3 1 10.5
3 2 89.5
一般来说,我可以使用相同的成分(ID
和重量百分比)使用不同的材料ID
,但同样重量百分比的相同成分ID可以是多个材料
我需要找到具有完全相同数量的成分,相同成分id和相同重量百分比的材料ID
(在材料ID 1和3的数据示例中)
最好的输出是:
ID Duplicate ID's
1 1,3
2 15,25
....
只是为了澄清这个问题:我有几千个材料,如果我只得到重复行的id,它就无法帮助我 - 我想看看是否有可能获得重复材料ID的组相同的行或字段。
答案 0 :(得分:3)
在包含所有成分的CTE中构建XML字符串,并使用该字符串来确定哪些材料是重复的。
MS SQL Server 2008架构设置:
create table Materials
(
material_id int,
constituent_id int,
constituent_wt_pct decimal(10, 2)
);
insert into Materials values
(1, 1, 10.5),
(1, 2, 89.5),
(2, 1, 10.5),
(2, 5, 15.5),
(2, 7, 74),
(3, 1, 10.5),
(3, 2, 89.5);
查询1 :
with C as
(
select M1.material_id,
(
select M2.constituent_id as I,
M2.constituent_wt_pct as P
from Materials as M2
where M1.material_id = M2.material_id
order by M2.constituent_id,
M2.material_id
for xml path('')
) as constituents
from Materials as M1
group by M1.material_id
)
select row_number() over(order by 1/0) as ID,
stuff((
select ','+cast(C2.material_id as varchar(10))
from C as C2
where C1.constituents = C2.constituents
for xml path('')
), 1, 1, '') as MaterialIDs
from C as C1
group by C1.constituents
having count(*) > 1
<强> Results 强>:
| ID | MATERIALIDS |
--------------------
| 1 | 1,3 |
答案 1 :(得分:0)
您可以使用以下代码获取重复值
Select EMP_NAME as NameT,count(EMP_NAME) as DuplicateValCount From dbo.Emp_test
group by Emp_name having count(EMP_NAME) > 1