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


Kindly advise , How to create query in sqlserver for below issue,


Kindly advise , How to create query in sqlserver for below issue,

Author
Message
s.chandrahasan
s.chandrahasan
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 106
Dear Team ,

Kindly advise , How to create query in sqlserver for below issue,

I Have Table with below details

Id Name Value1 value2 Total(Value1+Value2) PreviousRowId
1 A 10 0 10 0
2 B 5 1
3 C 6 2
4 D 7 3
5 E 8 4
6 F 5 5


Qustions :
1 . 1st row Total comes to second row Value1
then calculate 2nd row total . then its comes to 3rd row Value1 column. and its continue for all rows.
how to write the query for this issue?

Note : Link between all rows ==> Id , PreviousRowId


I need output like this below ,

Id Name Value1 value2 Total(Value1+Value2) PreviousRowId
1 A 10 0 10 0
2 B 10 5 15 1
3 C 15 6 21 2
4 D 21 7 28 3
5 E 28 8 36 4
6 F 36 5 41 5


Thanks,
Chandrahasan S
Calibear
Calibear
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 132
Please visit the following link to see how to best provide data for your questions:

http://www.sqlservercentral.com/articles/Best+Practices/61537/

--------
For SQL Tips, check out my blog: http://lantztechknowledge.blogspot.com/
You can also follow my twitter account to get daily updates: @BLantz2455
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6008 Visits: 5280
You will need a CTE to achieve the results

DECLARE   @table TABLE
(
Id INT,
Name VARCHAR(10),
Value1 INT,
value2 INT,
PreviousRowId INT
)

INSERT @table
SELECT 1, 'A', 10, 0, 0 UNION ALL
SELECT 2, 'B', 0, 5, 1 UNION ALL
SELECT 3, 'C', 0, 6, 2 UNION ALL
SELECT 4, 'D', 0, 7, 3 UNION ALL
SELECT 5, 'E', 0, 8, 4 UNION ALL
SELECT 6, 'F', 0, 5, 5

; WITH cte_table AS
(
SELECT t.Id, t.Name, t.Value1, t.value2, t.Value1 + t.value2 AS total, t.PreviousRowId
FROM @table AS t
WHERE t.PreviousRowId = 0
UNION ALL
SELECT t2.Id, t2.Name, ct.total, t2.value2, ct.total + t2.value2 AS total, t2.PreviousRowId
FROM @table AS t2
INNER JOIN cte_table AS ct ON t2.PreviousRowId = ct.Id
)

SELECT *
FROM cte_table




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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