我试图了解从当前承诺日期(currentprmdate)相对于承诺日期7天前(prmdate7), 14天前(prmdate14)和28天(prmdate28)前在工厂,分部,模型级别的天数的承诺日期的平均运动…我还想在这个聚合级别上找到ordernumber的不同数量。
计算的是平均天差(currentprmdate -prmdate7)
平均日差(currentprmdate -prmdate14)
平均日差(currentprmdate-prmdate28)
超过段、工厂和模型
输入表
segment | plant | model | currentprmdateprmdate7 | prmdate14 | prmdate28 | V89121 | vinots | Chikoo | HJ781 | 5/6/2021 | 5/5/2021 | 5/1/2021 | 5/7/2021 |
---|---|---|---|---|---|---|---|
LM12781 | vinots | Chikoo | HJ781 | 5/17/2021 | 5/11/2021 | 5/15/2021 | 5/10/2021 |
JK9812 | vinots | Chikoo | HJ781 | 5/3/2021 | 4/28/2021 | 4/25/2021 | 4/20/2021 |
LP18921 | Vimar | 朱莉 | MK241 | 4/3/2021 | 3/27/2021 | 3/21/2021 | 3/20/2021 |
BN1231 | Vimar | 朱莉 | MK241 | 6/10/2021 | 6/5/2021 | 6/3/2021 | 6/1/2021 |
LO1231 | Vimar | 朱莉 | MK241 | 7/15/2022 | 7/11/2022 | 7/13/2022 | 7/7/2022 |
;WITH cte AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY ordernumber ORDER BY orderdate) AS Rn
FROM #input i WHERE i.code1 IN ('SLD','SPP','SOB') OR i.code2 IN ('SLD','SPP','SOB'))
SELECT * FROM cte WHERE Rn = 1