如何避免重複值出現當嘗試從表中提取產品的最後一筆交易時。我的查詢,如下所示我的形象就像嘗試獲取產品的最後一筆交易時出現重複值
SELECT
a.branchid,
a.TellerID,
a.ProductID,
a.TransactDateTime,
a.ProductStock,
a.ProductStockInLocalCrrncy
FROM ALX_SubInventoryCashTransfers a
INNER JOIN (
SELECT
branchid,
TellerID,
ProductID,
MAX(TransactDateTime) datetime
FROM ALX_SubInventoryCashTransfers
GROUP BY branchid,
TellerID,
ProductID,
TransactDateTime
) tm
ON a.BranchID = tm.BranchID
AND a.branchid = tm.BranchID
AND a.TellerID = tm.TellerID
AND a.ProductID = tm.ProductID
AND a.TransactDateTime = tm.datetime
您能告訴我們包含重複項的實際輸出嗎?您包含的屏幕截圖顯示了多個'datetime'值,這是您的查詢所不可能的,因爲您將限制爲某個表中具有最大值的記錄。 –
嘗試從group by子句中刪除TransactDateTime字段。 –
@ tim,它是重複的。我只需要一個日期一個產品價值。你可以看到在圖片產品ID 2重複兩次在同一個說,我只需要它在一天中只有一次 –