![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am trying to create a stored procedure that will add the last two entries from a column. If I had a column named box, I would want to add the two values in box, for which my ID column is Max and Max-1. Is there anyone who can help me with this? Matt |
#3
| |||
| |||
|
|
select sum(box) from ( select top 2 box from your_table order by id desc) Cheers, Jason Lepack On Jul 26, 3:16 pm, mcolson <mcolson1... (AT) gmail (DOT) com> wrote: I am trying to create a stored procedure that will add the last two entries from a column. If I had a column named box, I would want to add the two values in box, for which my ID column is Max and Max-1. Is there anyone who can help me with this? Matt |
#4
| |||
| |||
|
|
On Jul 26, 2:21 pm, Jason Lepack <jlep... (AT) gmail (DOT) com> wrote: select sum(box) from ( select top 2 box from your_table order by id desc) Cheers, Jason Lepack On Jul 26, 3:16 pm, mcolson <mcolson1... (AT) gmail (DOT) com> wrote: I am trying to create a stored procedure that will add the last two entries from a column. If I had a column named box, I would want to add the two values in box, for which my ID column is Max and Max-1. Is there anyone who can help me with this? Matt so i should SET box = select sum(box) from ( select top 2 box from batchdaa order by IDnum desc) ? |
#5
| |||
| |||
|
|
On Jul 26, 2:43 pm, mcolson <mcolson1... (AT) gmail (DOT) com> wrote: On Jul 26, 2:21 pm, Jason Lepack <jlep... (AT) gmail (DOT) com> wrote: select sum(box) from ( select top 2 box from your_table order by id desc) Cheers, Jason Lepack On Jul 26, 3:16 pm, mcolson <mcolson1... (AT) gmail (DOT) com> wrote: I am trying to create a stored procedure that will add the last two entries from a column. If I had a column named box, I would want to add the two values in box, for which my ID column is Max and Max-1. Is there anyone who can help me with this? Matt so i should SET box = select sum(box) from ( select top 2 box from batchdaa order by IDnum desc) ? ahh the power of parenthesis box = (SELECT SUM(box) FROM batchdata WHERE IDnum >= (SELECT (MAX(IDnum)-1) FROM batchdata)) I had to make sure the parenthesis were all in the correct spot. Especially since I am doing this for 4 columns and there was a comma at the end- Hide quoted text - - Show quoted text - |
![]() |
| Thread Tools | |
| Display Modes | |
| |