Add last two values
am 26.07.2007 21:16:49 von mcolson
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
Re: Add last two values
am 26.07.2007 21:21:04 von jlepack
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 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
Re: Add last two values
am 26.07.2007 21:43:10 von mcolson
On Jul 26, 2:21 pm, Jason Lepack 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 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)
?
Re: Add last two values
am 26.07.2007 22:28:19 von mcolson
On Jul 26, 2:43 pm, mcolson wrote:
> On Jul 26, 2:21 pm, Jason Lepack 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 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
Re: Add last two values
am 30.07.2007 11:13:52 von masri999
On Jul 27, 1:28 am, mcolson wrote:
> On Jul 26, 2:43 pm, mcolson wrote:
>
>
>
>
>
> > On Jul 26, 2:21 pm, Jason Lepack 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 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 -
You need to make sure the following . (assuming you don't want to sum
duplicate values )
1. There are no duplicate maximum values
2. A row with maximum - 1 exists and this also should not have any
duplicates .
you may be looking for next to maximum ( not max - 1 ) . ie
Your values should not have gaps
8
6
5
3
In this case max = 8 , max - 1 = 7 does not exist