Dan*_*iel 0 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 |
+------+-----------+
Run Code Online (Sandbox Code Playgroud)
最终的结果我想获得3列.项目,项目2和百分比相似.所以看起来像:
+------+-------+-------------------+
| Item | Item2 | PercentageSimilar |
+------+-------+-------------------+
| AAA | ABC | 75% |
| AAA | CAA | 25% |
| ABC | AAA | 100% |
| ABC | CAA | 0% |
| CAA | AAA | 50% |
| CAA | ABC | 0% |
+------+-------+-------------------+
Run Code Online (Sandbox Code Playgroud)
这可能与SQL有关吗?
您可以使用a 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |