我有表中的數據如下圖所示:查詢返回Id列唯一但顯示號碼相同的數據?
CustomerId OrderId NoToDisplay
123 176 1
123 177 1
124 125 1
124 126 2
124 127 3
125 182 6
125 183 6
我想寫一個返回都得到了相同的NoToDisplay價值獨特訂單號查詢。因此,從數據集片段輸出上面會:
CustomerId OrderId NoToDisplay
123 176 1
123 177 1
125 182 6
125 183 6
這是我已經嘗試了查詢,但它沒有返回我的預期:
SELECT DISTINCT c.CustomerID, o.OrderID, o.NoToDisplay
FROM Customer c
LEFT OUTER JOIN Order o ON o.CustomerID = c.CustomerID
WHERE o.OrderID IN (SELECT DISTINCT o.NoToDisplay
FROM Customer c
LEFT OUTER JOIN Order o ON o.CustomerID = c.CustomerID
GROUP BY o.NoToDisplay HAVING COUNT(*) > 1)