SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sequence Number Generation


Sequence Number Generation

Author
Message
ch.ds19
ch.ds19
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 132
Hi all,

I have a column1 with values 1,2,3,4,5 and I want to add cumelatively the values into the target column as 1,3,6,10,15 , using ssis.

can anybody helpme out....

thanks in advance.
andrewd.smith
andrewd.smith
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1762 Visits: 3232
There is a standard mathematical expression for the sum of all integers from 1 to N.

Sum(R) {R = 1..N} = N * (N + 1) / 2



So you could just adapt this query in T-SQL for your purposes.
You will need a Tally (otherwise known as a Numbers table) to generate the sequence of consecutive integers.

SELECT T.N, T.N * (T.N + 1) / 2 AS SumOfN
FROM Tally T
WHERE (T.N BETWEEN 1 AND 10)



N           SumOfN      
----------- -----------
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55


EDIT: Just noticed you stated that you already had a column (named column1) containing consecutive integers. If so, and if this column is of integer type, then this query will probaly give you what you want.

SELECT column1, column1 * (column1 + 1) / 2 AS SumOfN
FROM MyTable


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search