如何找到具有类似组件的产品?

时间:2017-07-18 17:09:46

标签: sql sql-server subquery

我正在尝试查找其中包含至少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 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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%

答案 2 :(得分: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)