我是SQL Server数据库的新手,我还在学习基本的SQL。
请有人帮忙解决以下问题吗?
我的数据库表结构如下
现在我尝试编写一个查询来显示所有五个表中的一些记录。我目前的查询如下:
SELECT
Item.ItemCode, Item.ItemDescription,
Purchase.Date,
PurchaseDetails.GRNCode, PurchaseDetails.ReceivedQty,
Issue.Date AS Expr1,
IssueDetails.MRNCode, IssueDetails.IssueQty
FROM
Purchase
INNER JOIN
PurchaseDetails ON Purchase.GRNCode = PurchaseDetails.GRNCode
INNER JOIN
Issue
INNER JOIN
IssueDetails ON Issue.MRNCode = IssueDetails.MRNCode
INNER JOIN
Item ON IssueDetails.ItemCode = Item.ItemCode ON PurchaseDetails.ItemCode = Item.ItemCode
WHERE
(Item.ItemCode = 10001)
它将产生如下输出,但这里的问题是一些数据正在重复突出显示
请有人帮我执行上述查询,以显示如下数据和缩短日期
因为我的表结构可以执行吗?
如果可以请某人帮助我该怎么做。
谢谢!
答案 0 :(得分:1)
我为SQL做了一个尝试,这就是我想出的:
SELECT Item.ItemCode,
Item.ItemDescription,
Purchase.Date,
PurchaseDetails.GRNCode,
PurchaseDetails.ReceivedQty,
Issue.Date AS Expr1,
IssueDetails.MRNCode,
IssueDetails.IssueQty
FROM Purchase
INNER JOIN PurchaseDetails ON Purchase.GRNCode = PurchaseDetails.GRNCode
INNER JOIN Item ON PurchaseDetails.ItemCode = Item.ItemCode
INNER JOIN IssueDetails ON IssueDetails.ItemCode = Item.ItemCode
INNER JOIN Issue ON Issue.MRNCode = IssueDetails.MRNCode
WHERE (Item.ItemCode = 10001)
GROUP BY PurchaseDetails.GRNCode
ORDER BY Purchase.Date asc;
在加入问题表时,您似乎忘记了您的ON子句。
我添加了ORDER BY Purchase.Date asc子句,按日期按升序排列结果。
请告诉我这是否对您有所帮助:)。