2017-07-18 40 views
0

我正在尝试查找其中至少有75%+类似组件的项目,我们有成千上万种产品。我的表有2列,Item和Component。例如:如何查找含有类似组件的产品?

+------+-----------+ 
| Item | Component | 
+------+-----------+ 
| AAA | screw  | 
| AAA | metal  | 
| AAA | bar  | 
| AAA | nut  | 
| ABC | screw  | 
| ABC | metal  | 
| ABC | bar  | 
| CAA | nut  | 
| CAA | cap  | 
+------+-----------+ 

最终结果我想得到3列。项目,项目2和百分比类似。所以它看起来像:

+------+-------+-------------------+ 
| Item | Item2 | PercentageSimilar | 
+------+-------+-------------------+ 
| AAA | ABC | 75%    | 
| AAA | CAA | 25%    | 
| ABC | AAA | 100%    | 
| ABC | CAA | 0%    | 
| CAA | AAA | 50%    | 
| CAA | ABC | 0%    | 
+------+-------+-------------------+ 

这可能与SQL有关吗?

+3

如何是 “ABC” 类似的75%为 “AAA”, “CAA是只有25%” 类似 “AAA” - 和(我的最爱)“ ABC“是0%”与“CAA”类似吗?你想做什么? –

+1

您的预期结果毫无意义......您能否澄清? –

+0

@StanShaw,让我感觉我认为同类应该来自螺杆和螺母等。 – user6144226

回答

0

在这里你去 - 比你更多的信息要求,但在这里被击穿,因此您可以了解如何实现这一结果:

安装程序与样品数据:

DECLARE @ItemsAndComponents TABLE 
    (
     Item VARCHAR(3), 
     Component VARCHAR(50) 
    ) 

INSERT INTO @ItemsAndComponents 
VALUES 
('AAA', 'screw'), 
('AAA', 'metal'), 
('AAA', 'bar'), 
('AAA', 'nut'), 
('ABC', 'screw'), 
('ABC', 'metal'), 
('ABC', 'bar'), 
('CAA', 'nut'), 
('CAA', 'cap') 

查询:

SELECT DISTINCT  
     T1.Item AS [First Item], 
     T2.Item AS [Second Item], 
     SUM(CASE WHEN T1.Component = T2.Component THEN 1 ELSE 0 END) AS [Matches], 
     COUNT(distinct T1.Component) AS [Total], 
     CAST(100. * SUM(CASE WHEN T1.Component = T2.Component THEN 1 ELSE 0 END)/COUNT(distinct T1.Component) AS DECIMAL(18, 2)) AS [Percent Similar] 
FROM @ItemsAndComponents T1 
JOIN @ItemsAndComponents T2 
    ON T1.Item <> T2.Item 
GROUP BY T1.Item, T2.Item 
ORDER BY T1.Item, T2.Item 

结果:

First Item Second Item Matches  Total  Percent Similar 
---------- ----------- ----------- ----------- --------------------------------------- 
AAA  ABC   3   4   75.00 
AAA  CAA   1   4   25.00 
ABC  AAA   3   3   100.00 
ABC  CAA   0   3   0.00 
CAA  AAA   1   2   50.00 
CAA  ABC   0   2   0.00 

(6 row(s) affected) 
+1

这确实让它更容易理解,谢谢! – Daniel

5

您可以使用self join来执行此操作。

select t1.item,t2.item 
,100.*count(case when t1.component=t2.component then 1 end) 
/count(distinct t1.component) as pct_similar 
from t t1 
join t t2 on t1.item<>t2.item 
group by t1.item,t2.item 
0
--creating table 
with myTable as (
select 'AAA' as Item , 'screw' as Component union 
select 'AAA' as Item , 'metal' as Component union 
select 'AAA' as Item , 'bar' as Component union 
select 'AAA' as Item , 'nut' as Component union 
select 'ABC' as Item , 'screw' as Component union 
select 'ABC' as Item , 'metal' as Component union 
select 'ABC' as Item , 'bar' as Component union 
select 'CAA' as Item , 'nut' as Component union 
select 'CAA' as Item , 'cap' as Component 
) 

--Query 
select distinct a.item , b.Item as Item2 , 
cast((select count(*) from myTable as x inner join myTable as y on x.Item = a.Item and y.Item = b.Item and x.Component = y.Component) *100/
(select count(*) from myTable where Item = a.item) as nvarchar(3)) +'%' as Percentage 
from myTable as a inner join 
myTable as b on a.Item <> b.item 

这里是结果:

item Item2 Percentage 
---- ----- ---------- 
AAA ABC 75% 
AAA CAA 25% 
ABC AAA 100% 
ABC CAA 0% 
CAA AAA 50% 
CAA ABC 0% 
+0

这是完美的!非常感谢你的帮助 – Daniel

相关问题