2016-04-15 80 views
1

我對SQL很陌生,這是我第一次嘗試編寫查詢。SQL選擇重複結果問題

我已經成功地將我需要的數據與下面的數據相提並論,但是由於每個項目有多個useddates我的結果顯示每個項目有多個實例。我試過用MAX確認我是否正確。

預期結果:每個項目只能看到一條最新的lastuseddate

use bosext 
SELECT artno, shorttext, longtext, price,MAX(lastuseddate) as LastUsed 
FROM accountitem, accountitemdyn 
WHERE NOT EXISTS 
    (SELECT * 
    FROM shelfitem 
    WHERE accountitem.ID = shelfitem.itemID) 
Group by accountitem.artno,shorttext, longtext, price, lastuseddate 

enter image description here

+0

後所有表樣本數據 – Matt

+0

勉力讓它與以下指導一起工作:)謝謝 – S3renity

回答

0

使用組由不lastuseddate

Group by accountitem.artno,shorttext, longtext, price 
0

使用FULL OUTER JOIN而不是WHERE EXISTS,用正確的連接加入你的兩個賬戶表,不包括lastuseddateGROUP BY,因爲它已經在一個聚合函數中。

USE bosext 
SELECT ai.artno, shorttext, longtext, price, MAX(lastuseddate) AS LastUsed 
FROM accountitem ai 
INNER JOIN accountitemdyn aid ON = ai.? = aid.? 
FULL OUTER JOIN shelfitem s ON ai..ID = s.itemID 
Group by ai.artno, shorttext, longtext, price 
0

你不應該包括在組lastuseddate by子句

use bosext 
SELECT artno, shorttext, longtext, price,MAX(lastuseddate) as LastUsed 
FROM accountitem, accountitemdyn 
WHERE NOT EXISTS 
    (SELECT * 
    FROM shelfitem 
    WHERE accountitem.ID = shelfitem.itemID) 
Group by accountitem.artno,shorttext, longtext, price 
0

Group By條款

刪除lastuseddate嘗試以下方法

SELECT artno, shorttext, longtext, price,MAX(lastuseddate) as LastUsed 
FROM accountitem, accountitemdyn 
WHERE NOT EXISTS 
    (SELECT * 
    FROM shelfitem 
    WHERE accountitem.ID = shelfitem.itemID) 
Group by accountitem.artno,shorttext, longtext, price