SQL Query返回重复的组件记录

时间:2015-05-18 17:30:42

标签: sql-server

我有三个表,每个表按作业号相关:OrderHeader,OrderComponent,OrderQtyTable。此订单有五个组件,当运行此查询时,它返回25行,使用TotalPrice复制每个组件五次,为五个组件中的每一个显示五个组件中的每一个。

SELECT 
    DISTINCT TOP (100) PERCENT dbo.OrderHeader.JobNumber,
    dbo.OrderComponent.ComponentNumber AS Comp,
    dbo.OrderHeader.JobDescription,
    dbo.OrderComponent.Description,
    dbo.OrderQtyTable.Quantity,
    dbo.OrderQtyTable.TotalPrice
FROM dbo.OrderHeader
INNER JOIN dbo.OrderComponent ON dbo.OrderHeader.JobNumber = dbo.OrderComponent.JobNumber
INNER JOIN dbo.OrderQtyTable ON dbo.OrderHeader.JobNumber = dbo.OrderQtyTable.JobNumber
WHERE (dbo.OrderHeader.JobNumber = '97180'

1 个答案:

答案 0 :(得分:1)

将JOIN更改为OrderQty应该可以消除重复:

INNER JOIN dbo.OrderQtyTable 
ON dbo.OrderHeader.JobNumber = dbo.OrderQtyTable.JobNumber
AND dbo.OrderComponent.ComponentNumber = dbo.OrderQtyTable.ComponentNumber