[英]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.