我有一个 SQL 查询,我必须查询三个表(其中一些表两次),但我没有得到我需要的结果:
我需要在“文章”中搜索搜索字符串(“网球”)并为这些文章查找第二个表“订单详细信息”。点击显示订购了多少。
现在我需要检查这些物品是否已经交付。因此,我在“orderdetails”表中查找“orders”,如果这些订单有“delivery”,则查看同一张表。'orders' 和 'delivery' 有一个字段显示它是什么类型。
所以我必须检查“交货”是否由“订单”转发。
接下来检查这个'delivery'是否包含'article'并总结交付的文章。
如果交付的文章数量低于订购的文章数量我想显示这条记录。
到目前为止,除了这些项目之外它还有效: - [已解决] 感谢 HLGEM 没有显示“交货”的“订单” - [已解决] 显示与“交货”相同数量的“订单”,但我没有希望他们。
这是我到目前为止所拥有的:
PrO:流程订单
PrD:流程交付
a:ArticleItem
p:processOrderItem
d:deliveryItem
[更新代码]
SELECT
a.Articlenumber AS Article,
PrO.Number AS Order,
PrD.Number AS Delivery,
p.Amount AS Orderamount,
SUM(d.Amount) AS Deliveryamount,
(p.Amount - Deliveryamount) AS OpenAmount
FROM Article AS a
INNER JOIN ProcessesDetails AS p
ON (a.ArticleNumber = p.Article)
AND LEFT(p.Order, 3) = 'OR-'
INNER JOIN Processes as PrO
ON PrO.Number = p.Order
AND TEXTSEARCH('Delivery:' IN PrO.Forwarded)
LEFT JOIN Processes as PrD
ON PrO.Nummer = PrD.ForwardedFrom
AND LEFT(PrD.Number,3) = 'DE-'
INNER JOIN ProcessesDetails as d
ON PrD.Number = d.Order
AND d.Article = p.Article
WHERE (a.Categorie = 'tennis')
GROUP BY(Article)
添加以下行解决了第二个问题:
HAVING Deliveryamount < Orderamount