![](/img/trans.png)
[英]Sql Query to find duplicates in 2 columns where the values in first column are same
[英]Access SQL Query: Making two columns of the same values, but how do you exclude cases where column 1 = column 2?
这是我的数据的模型:
Product || MFGR
A || 1
A || 2
B || 1
B || 3
从这些数据中,我想找到所有可能的“交换”组合。 例如,产品A可以以MFGR 1或2进行生产。同样,产品B可以以MFGR 1或3进行生产。据此,我想生成下表:
MFGR1 || MFGR2
1 || 2 \______ Swaps for Product A
2 || 1 /
1 || 3 \______ Swaps for Product B
3 || 1 /
我已经非常接近以下SQL:
SELECT DISTINCT
[Products].MFGR AS MFGR1,
[Products_1].MFGR AS MFGR2
FROM [Products]
LEFT JOIN [Products] AS [Products_1] ON [Products].[Product] = [Products_1].[Product];
但是,这有望报告MFGR1 = MFGR2的情况:
MFGR1 || MFGR2
1 || 1 ← want removed
1 || 2
2 || 1
2 || 2 ← want removed
1 || 3
3 || 1
3 || 3 ← want removed
我所做的第一件事是进入设计视图,并将条件=Not(MFGR1)
到MFGR2。 那产生了这个SQL: HAVING ((([Products_1].MFGR)=Not ([PRODUCTS_1].MFGR)=([MFGR1])));
但是当我尝试查看它时,我得到了HAVING子句[name]而不进行分组或聚合。
我加了GROUP BY MFGR1, MFGR2
喜欢这篇文章建议,但后来我得到Your query does not include the specified expression 'MFGR1' as part of an aggregate function
, 但是从我所看到的 ,这个错误应该包括解决MFGR1
在我GROUP BY
。
我的最后努力是在我的初始查询(没有任何条件)的基础上创建一个新查询。 这是用于此的SQL:
SELECT [Product Query].MFGR1, [Product Query].MFGR2
FROM [Product Query]
WHERE ((([Product Query].MFGR2)=Not ([Product Query].MFGR2)=([MFGR1])));
但是,这会使所有行消失。
尝试将[Products].MFGR <> [Products_1].MFGR
到您的加入条件中:
SELECT DISTINCT
[Products].MFGR AS MFGR1,
[Products_1].MFGR AS MFGR2
FROM [Products]
INNER JOIN [Products] AS [Products_1] ON [Products].[Product] = [Products_1].[Product]
AND [Products].MFGR <> [Products_1].MFGR;
将LEFT JOIN
更改为INNER JOIN
以防止结果集中出现一堆NULL
值。 我想这就是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.