SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Addition Of previous two rows into new row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Shadab Shah
Starting Member

India
23 Posts

Posted - 07/19/2012 :  01:00:33  Show Profile  Reply with Quote
I am a newbie and was trying around and come up with this problem:
Suppose i have a table A having column as:
col1
100
350
-120
100

i want the output as
col1
100
450
330
430

The output is actually addition of 100+350=450,450+(-120)=330,330+100=430.

And i have only this column in my table.

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 07/19/2012 :  02:35:09  Show Profile  Reply with Quote
how do you determine the order ?

There isn't any specific sequence or order of how record is stored in the table. You determine how do you want the records to return using the ORDER BY clause in the SELECT statement



KH
Time is always against us

Go to Top of Page

Shadab Shah
Starting Member

India
23 Posts

Posted - 07/19/2012 :  02:49:11  Show Profile  Reply with Quote
Now that's the challenge over here. I want to achieve the above output with-out binding it with order by or groupby in the select statement. Actually i don't know whether this can be achieve though SQL Server or not
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 07/19/2012 :  02:58:19  Show Profile  Reply with Quote
quote:
Originally posted by Shadab Shah

Now that's the challenge over here. I want to achieve the above output with-out binding it with order by or groupby in the select statement. Actually i don't know whether this can be achieve though SQL Server or not



if you have only one column, how do you determine the required sequencing of the record return ?



KH
Time is always against us

Go to Top of Page

Shadab Shah
Starting Member

India
23 Posts

Posted - 07/19/2012 :  05:42:20  Show Profile  Reply with Quote
i think if i am able to understand what you mean to say then my answer would be : The sequence should be 100 , 450(100+350) ,330(450+(-120)) , 430(330+100)
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 07/19/2012 :  06:03:36  Show Profile  Reply with Quote
quote:
Originally posted by Shadab Shah

i think if i am able to understand what you mean to say then my answer would be : The sequence should be 100 , 450(100+350) ,330(450+(-120)) , 430(330+100)



not exactly. what i am saying is, in order to get the records to return in the same ordering manner every time you retrieve it is to use the ORDER BY in the query
example
SELECT col1 FROM yourtable ORDER BY col1
or
SELECT col1 FROM yourtable ORDER BY col1 DESC

if you want it to be in descending order.

Without that ORDER BY clause, the sequence of records return is not guaranteed.

As you said you have only one column in that table, you can only ORDER BY col1. And the result is not what you might want it to be. Unless you have a ID identity column in that table, than it will be a different story
example :

ID  col1
1   100
2   350
3   -120
4   100


then you can order by that ID column
SELECT ID, col1 FROM yourtable ORDER BY ID


KH
Time is always against us

Go to Top of Page

Shadab Shah
Starting Member

India
23 Posts

Posted - 07/19/2012 :  06:12:56  Show Profile  Reply with Quote
I may still be not able to get you but over here i want addition of previous rows and i don't see any interlink between them
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000