dbTalk Databases Forums  

Add last two values

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Add last two values in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mcolson
 
Posts: n/a

Default Add last two values - 07-26-2007 , 02:16 PM






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


Reply With Quote
  #2  
Old   
Jason Lepack
 
Posts: n/a

Default Re: Add last two values - 07-26-2007 , 02:21 PM






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:
Quote:
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



Reply With Quote
  #3  
Old   
mcolson
 
Posts: n/a

Default Re: Add last two values - 07-26-2007 , 02:43 PM



On Jul 26, 2:21 pm, Jason Lepack <jlep... (AT) gmail (DOT) com> wrote:
Quote:
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)
?



Reply With Quote
  #4  
Old   
mcolson
 
Posts: n/a

Default Re: Add last two values - 07-26-2007 , 03:28 PM



On Jul 26, 2:43 pm, mcolson <mcolson1... (AT) gmail (DOT) com> wrote:
Quote:
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



Reply With Quote
  #5  
Old   
M A Srinivas
 
Posts: n/a

Default Re: Add last two values - 07-30-2007 , 04:13 AM



On Jul 27, 1:28 am, mcolson <mcolson1... (AT) gmail (DOT) com> wrote:
Quote:
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 -
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




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.