I have a table that has [Order], [Yield], [Scrap], [OpAc] columns. I need to pull the yield based on the max value of [OpAc].
Order | Yield | Scrap | OpAc |
---|---|---|---|
1234 | 140 | 0 | 10 |
1234 | 140 | 0 | 20 |
1234 | 130 | 10 | 30 |
1234 | 130 | 0 | 40 |
1234 | 125 | 5 | 50 |
1234 | 110 | 15 | 60 |
1235 | 140 | 0 | 10 |
1235 | 138 | 2 | 20 |
1235 | 138 | 0 | 30 |
1235 | 138 | 0 | 40 |
1235 | 138 | 0 | 50 |
1235 | 137 | 1 | 60 |
1235 | 137 | 0 | 70 |
Expected Results
Order | Yield |
---|---|
1234 | 110 |
1235 | 137 |
The query that I have tried is
select [Order], [Yield], MAX([OpAc]) as Max_OpAc
from SCRAP
GROUP BY [Order], [Yield]
order by [order]
This produces
Order | Yield | Max_OpAc |
---|---|---|
1234 | 110 | 60 |
1234 | 125 | 50 |
1234 | 130 | 40 |
1234 | 140 | 20 |
1235 | 137 | 70 |
1235 | 138 | 50 |
1235 | 140 | 10 |
I've tried setting up some CTE queries to break it down into separate functions but I keep getting caught at this step.
WITH CTE1 AS(
SELECT ROW_NUMBER() OVER(PARTITION BY [Order] ORDER BY [Order],[OpAc]) AS RN , *
FROM SAP_SCRAP
),
This proved to be redundant due to the fact that the [OpAc] field is sequential for each step.
Thanks in advance for any help