我已经把头敲了很长时间,以解决这个问题。如果有人可以帮助我这样做: -
我有三列Recipe, alt recipe
和Component
。结构如下: -
Recipe Alt_Recipe Recipe_component
1430 1 110000000
1430 1 170000051
1430 2 110000000
1430 2 170000051
1430 3 110000000
1430 3 170000051
我需要找到在任何替代食谱中重复组件(全部)组件(全部)。例如: - 食谱1430在Alt食谱中具有2个组件1,即 110000000 和 170000051 。如果在任何其他Alt食谱中找到这两个(例如2或3),我需要指出它们。另外,如果Alt食谱3与其他一些组件具有罚款,则不应指出其他组件。
非常感谢我带来的任何帮助!:)
您可以通过自我加入和聚合来执行此操作:
select rc1.Recipe, rc1.Alt_Recipe, rc2.Alt_Recipe as hasSameComponents
from recipecomponents rc1 left join
recipecomponents rc2
on rc1.recipe = rc2.recipe and rc1.Component = rc2.Component and
rc1.Alt_Recipe <> rc2.Alt_Recipe
group by rc1.Recipe, rc1.Alt_Recipe, rc2.Alt_Recipe
having count(*) = count(rc2.recipe);
逻辑是匹配每对" ALT"配方的组件。然后只需计算与第一个匹配的数字。
喜欢吗?
select recipe, count(*)
from dbo.receipts
group by recipe, alt_recipe, recipe_component
having count(*) > 1
通过与Oracle使用ListAgg的操作相似。
- 通过使用XML路径(创建数据集) 的组件组件通过Alt配方组件
- 通过做配方,计数(不同的alt) 创建理想计数的数据集
- 基于食谱和count1&lt;> count2。 的数据集加入数据集
- 结果数据集是例外列表。
感谢大家的建议!:)