Category Table have:
- category code
- category name
Items table have:
- category code
- Item Code
- Item Name
Old Table have :
- Category Code
- Old code
- Old Detail
现在我要以格式
显示表格 Category Code | Category Name | Item Code | Item Name | Old Code | Old Detail
Š
所有三个表都有相同的类别代码
我使用了这个查询: -
SELECT dbo.N_items.CategoryCode, dbo.N_items.ItemCode AS [New Code], dbo.N_items.ItemDetail AS [New Detail], dbo.OldMasterTable.OldItemCode AS [Old Code], dbo.OldMasterTable.OldItemDetails AS [Old Detail]
FROM dbo.OldMasterTable INNER JOIN dbo.N_items
ON dbo.N_items.CategoryCode = dbo.OldMasterTable.CategoryCode
但项目细节表正在重复
答案 0 :(得分:0)
尝试使用GROUP BY
,也使用别名来轻松:
SELECT C.CategoryCode,
I.ItemCode AS [New Code],
I.ItemDetail AS [New Detail],
O.OldItemCode AS [Old Code],
O.OldItemDetails AS [Old Detail]
FROM Category C JOIN
Items I ON C.CategoryCode=I.CategoryCode JOIN
OldMasterTable O ON C.CategoryCode=O.CategoryCode
GROUP BY C.CategoryCode, I.ItemCode AS [New Code],
I.ItemDetail AS [New Detail], O.OldItemCode AS [Old Code],
O.OldItemDetails AS [Old Detail]
答案 1 :(得分:0)
尝试以下解决方案 - 它不会重复项目表详细信息。在加入之前先保留项目表。希望它能解决你的问题。
SELECT c.CategoryCode, c.CategoryName, i.ItemCode, i.ItemName, o.OldItemCode, o.OldItemDetails
FROM Items i
LEFT JOIN CategoryTable c on i.CategoryCode = c.CategoryCode
LEFT JOIN OldMasterTable o ON i.CategoryCode = o.CategoryCode