请有人帮助解决此问题:
查询table1并返回所有table1.parentOrderNumber,table1.orderPriority,table1.orderQuantity值,其中对应的table2.orderNumber等于table1.parentOrderNumber,最低值table2.operationNumber有一个table2.scheduledStartDate大于或等于当前日期-5天且小于或等于当前日期+3天。
这里table2有重复的orderNumber,它们对它们有不同的operationNumber。我试过有条款和其他但它似乎没有帮助。到目前为止,我已经编写了以下查询,它返回了重复的parentOrderNumber,而不仅仅是operationNumber的最小值:
SELECT
t1.parentOrderNumber AS parentOrderNumber
,t2.scheduledStartDate AS scheduledStartDate
,t1.salesOrderNumber AS salesOrderNumber
,t1.salesOrderLine AS salesOrderLine
,t1.orderPriority AS orderPriority
,t1.orderQuantity AS orderQuantity
,t1.modelNumber AS modelNumber
,t2.calculatedActualLabor AS CalculatedActualLabor
,MIN(t2.operationNumber) AS operationNumber
FROM
table2 t2 WITH(NOLOCK)
LEFT JOIN
table1 t1 WITH(NOLOCK) ON t2.orderNumber = t1.parentorderNumber
WHERE
parentOrderNumber <> ''
AND t1.productionplant = 'xyz'
GROUP BY
--t1.orderNumber
t1.parentOrderNumber
,t2.scheduledStartDate
,t1.salesOrderNumber
,t1.salesOrderLine
,t1.orderPriority
,t1.orderQuantity
,t1.modelNumber
,t2.calculatedActualLabor
答案 0 :(得分:0)
如果我正确理解您的问题,以下查询可以为您提供所需内容:
select * from t2 inner join
(select t2.ordernumber, min(t2.operationnumber) as operationnumber
from t2 where t2.schedulestartdate between getdate()-5 and getdate()+3
group by ordernumber) t2_1
on t2.ordernumber = t2_1.ordernumber and t2.operationnumber = t2_1.operationnumber
inner join t1 on t1.parentordernumber = t2.ordernumber