Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Multiple Sums and Last_Value() Help.

777349Jun 5 2010 — edited Jun 6 2010
This situation may be a little difficult to describe but bare with me. I have a database which shows the amount of labor done on a part and the columns I am retrieving from the database are as follows:

PART....... WORK_ORDER..... STEP.... OPERATION...... GOOD_PARTS

There are multiple parts in the database, each with multiple work orders, with multiple steps but each step only has 1 operation but each operation has multiple good_part entries.

What I need from this is to sum the sum of all good_parts for the last step of each work order but only if it is a certain operation for each part.

So say i have this data:
PART..... WORK_ORDER..... STEP..... OPERATION.... GOOD_PARTS
XYZ... .... .... 1.. ... .. ... ... 1.. .. .. .. GRIND.. .. . .. . . 50
XYZ... .... .... 1.. ... .. ... ... 1.. .. .. .. POLISH.. .. . .. .. 45
XYZ.. . ... ... .. 2 . . . .. .... .. .1.. .. ... ...GRIND. .. ... ... .. 40
XYZ. . .. .... ... 2 . .. . .. . ... . 2. . .. . ... POLISH .... .... ....45
XYZ... .. .... ... 2. ..... .... ... . 2... .... .... POLISH .. ... ... ... 5
XYZ . ..... ... .. 3.. ... .. .... ... .1. .. .... ... GRIND. .... ... .. . 40
XYZ. ... ... .. .. 3. .. .. .. .. ... . 2. .. ... ... POLISH. .. ... ... .. 45
XYZ. ... .... .. .. 3. .. .. ..... ... 3 . .. .. .. . SHIP. . .. .. .. .. .. 45

(This is the result of a bunch of joins and filters but it is essentially a SELECT * with the clause "ORDER BY PART, WORK_ORDER, STEP" to sort it correctly.)

And I want only the parts for "POLISH" i want to have the final sum of 95 because work order 1 has the last entry of "POLISH" with 45 parts; work order 2 has 2 entries of "POLISH" (which is the last step done) with a sum of 50; but work order 3 has the last operation "SHIP" so I don't include it at all.

I have no idea how to do this. I have previously pulled all this data into excel and ran a macro to calculate it but now I find that I need it to be calculated by the server. This database is Oracle, so I can use any functions it supports. I saw the function LAST_VALUE that might be helpful but I am not sure how to use it. Any help is greatly appreciated. Thanks in advance!

Please let me know if you need any more information
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2010
Added on Jun 5 2010
4 comments
1,283 views