2016-04-15 78 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