• Answers
  • Web
Personalize Yedda, (And make Danny Happy)
People ask & answer about almost everything. Tell us what you're interested in... So we can personalize Yedda especially for you
I'm interested in:

Ordinary People Discovering Extraordinary Abilities

Not So Smart Builder

How do I write a select query in SQL Server 2005 ...

How do I write a select query in SQL Server 2005 that calculates a value with the previous record value?

I have a select that returns a number. I would like to have another field that represents the current number + the previous record number.

Any suggestions?


Share Send to a friend Watch Report
 

Best Answer

 

I think you can run the following query:

select

name,

(select sum(value) from TABLENAME tn2 where

tn2.name <= tn.name) as sumValue

from TABLENAME tn

order by name

try this and let me know

Posted 2006-08-31T08:10:03Z
gliksman was invited by Yedda to answer this question.

Helpful?(4)
Rated as Best Answer

 

All Answers
Order by

 

a good way to do this would be to create an inner join of the table to itself in the following syntax:

select a1.value, a2.value

from tablename a1

inner join tablename a2

on a1.commonfield=a2.commonfield

where a1.id < a2.id

for a more specific answer i will need to understand the table structure and some sample data in it

Hope this helps

Ohad

Posted 2006-08-30T15:28:32Z
gliksman was invited by Yedda to answer this question.

 
26 helpful answers

Ordinary People Discovering Extraordinary Abilities

Not So Smart Builder

Thanks for the reply. I did not understand the query.

 Here's some sample data:

NameValuea3b7c2

On this table I would like to perform a select that will return:

NameTotala3b10c12

Where total is the sum of the current field value with the previous record value.

Thanks.

Posted 2006-08-30T16:51:50Z
 

can there be more then 1 previous records?

must there be a previous record?

Please reply with the table's full description and I will try to help you

Posted 2006-08-31T05:50:34Z
gliksman was invited by Yedda to answer this question.

 
26 helpful answers

Ordinary People Discovering Extraordinary Abilities

Not So Smart Builder

I see that the <table> tag is not working when viewing the post...

Here's a sample data (image):

Data

Posted 2006-08-31T06:09:57Z
 

The image is not shown

Posted 2006-08-31T06:37:07Z
gliksman was invited by Yedda to answer this question.

 
26 helpful answers

Ordinary People Discovering Extraordinary Abilities

Not So Smart Builder

Once again...

Name  -->  Value

a --> 4

b --> 8

c --> 3

d --> -6

Should output:

Name  -->  Total

a --> 4 (4 + 0, there is no previous row)

b --> 12 (8 + 4)

c --> 15 (12 + 3)

d --> 9 (15 - 6)

I hope you can see this.

Posted 2006-08-31T06:45:26Z
 
26 helpful answers

Ordinary People Discovering Extraordinary Abilities

Not So Smart Builder

Thanks, it worked great!

Posted 2006-08-31T08:54:48Z
 

Glad I could help

Posted 2006-08-31T09:05:12Z
gliksman was invited by Yedda to answer this question.

Sign in to participate

Got an answer for dudushmaya? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


Q:

Asynchronous jobs in sql server

Is there a way to run asynchronous jobs in sql server? I'm looking for an equivalent to Oracle's dbms_job.
Submitted by beytz   2 years ago.
  • viewed 468 times


Q:

How do I protect my self from sql injuction ...

How do I protect my self and my site from sql injuction?
Submitted by del2   3 years ago.
  • viewed 1025 times
Last answer posted 3 years ago by leonid


Q:

How to deploy database schema into hosting account

I'm developing an asp.net web site using VS 2005 & sql server express. Is it possible to deploy the website with the db schema ...
Submitted by beytz   2 years ago.
  • viewed 426 times
Last answer posted 2 years ago by kitt



» More...

Feed - Subscribe to changes to this Q&A Blog
ADVERTISEMENT
  • Answers
  • Web
Copyright © 2006-2009, Yedda Inc. and respective copyright owners · CC License