metropia
asked on
Modifying a procedure that uses CTEs
I would like to modify a stored procedure that uses multiple CTEs. The code works good, but I noticed something that needs to be added to the code, but I have not figured out how, and would like to get some help if possible.
Using the data and scripts I uploaded, I see that order S20026, with Original Quantity of 40,000, gets updated (by user) to 23,000, then the quantity shipped is 24,450
The code uses the 40,000 as the Original Quantity to calculate the Filled Rate, and that part of the code works excellent, but what would I need to "tweak" to the code is that for Orders like this one, with multiple changes in the Order Quantity, I want to use the last change that is not equal to the Quantity Shipped as the "Original Quantity" for order S20026 that would be 23,000
Example:
Order S20026
Order Quantity
40,000
23,000
24,450 = to ship qty of 24,450 then 23,000 is the original quantity.
Filled Rate = (24450 / 23,000) * 100 = 106.3
Order S19557
Order Quantity
38,000
39,000 = to ship qty of 39,000 then 38,000 is the original quantity.
Filled Rate = (39000 / 38,000) * 100 = 102.6
The code as is, already works well for order S19557, and S19856.
I think I just need to modify this CASE statement:
But I have not made it work yet.
The last ordered quantity comes from the last time an order quantity got updated that is not equal to the final shipped quantity. for order S20026, the last ordered quantity (requested by client) was 23,000 and the filled rate would be much higher using that qty than the 40,000
At any rate, thank you greatly for all your help.
script-create-insert.sql
script-cte.sql
stored-procedure.txt
Using the data and scripts I uploaded, I see that order S20026, with Original Quantity of 40,000, gets updated (by user) to 23,000, then the quantity shipped is 24,450
The code uses the 40,000 as the Original Quantity to calculate the Filled Rate, and that part of the code works excellent, but what would I need to "tweak" to the code is that for Orders like this one, with multiple changes in the Order Quantity, I want to use the last change that is not equal to the Quantity Shipped as the "Original Quantity" for order S20026 that would be 23,000
Example:
Order S20026
Order Quantity
40,000
23,000
24,450 = to ship qty of 24,450 then 23,000 is the original quantity.
Filled Rate = (24450 / 23,000) * 100 = 106.3
Order S19557
Order Quantity
38,000
39,000 = to ship qty of 39,000 then 38,000 is the original quantity.
Filled Rate = (39000 / 38,000) * 100 = 102.6
The code as is, already works well for order S19557, and S19856.
I think I just need to modify this CASE statement:
, CASE
WHEN cte2.ShippedQuantity = 0
THEN 0
ELSE cte_oqty.QuantityOrdered
END AS OrgQty
But I have not made it work yet.
The last ordered quantity comes from the last time an order quantity got updated that is not equal to the final shipped quantity. for order S20026, the last ordered quantity (requested by client) was 23,000 and the filled rate would be much higher using that qty than the 40,000
At any rate, thank you greatly for all your help.
script-create-insert.sql
script-cte.sql
stored-procedure.txt
ASKER
Hi Sharath,
the results for order's s19557 and s20026 scenarios are great.
order s19856 shows 'null' on the filled rate col. when it should be 100%
the results for order's s19557 and s20026 scenarios are great.
order s19856 shows 'null' on the filled rate col. when it should be 100%
Use COALESCE or ISNULL to replace null with 100
ASKER
Hi Sharath,
I did the ISNULL function and I think that can work for now with my problem.
I still find another problem, the line that contains the filled rate
example:
S20026 10568 2014-02-21 00:00:00.000 2014-03-14 00:00:00.000 2014-03-14 00:00:00.000 24450.000000000000000000 2014-03-14 00:00:00.000 24450.000000000000000000
It uses 24,450 as the quantity ordered, but it should show 23,000 because it uses that quantity to calculate the filled rate and it will be the line to be displayed on the report. I would like to see that quantity, the quantity used for the filled rate calc. as the quantity ordered.
I hope this makes sense.
Thank you very much for your help.
I did the ISNULL function and I think that can work for now with my problem.
I still find another problem, the line that contains the filled rate
example:
S20026 10568 2014-02-21 00:00:00.000 2014-03-14 00:00:00.000 2014-03-14 00:00:00.000 24450.000000000000000000 2014-03-14 00:00:00.000 24450.000000000000000000
It uses 24,450 as the quantity ordered, but it should show 23,000 because it uses that quantity to calculate the filled rate and it will be the line to be displayed on the report. I would like to see that quantity, the quantity used for the filled rate calc. as the quantity ordered.
I hope this makes sense.
Thank you very much for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Sharath. You have saved me big time!
Open in new window
My results areOpen in new window