SQL server - Using self join/Inner join get the data by adding all the above rows data

I need urgent help with reference to SQL server, the query details is as mentioned below:

Need to get the calculative column whose result will be addition of all the above rows. And I can't use correlated queries, Lag and Lead as it is not supported. I tried using Selfjoin/inner join/left outer join, the only problem I am facing is due to group by clause of other columns the result is not coming as expected..

For example,

Data is like alt text

Expected Output alt text

But the output I am getting due to group by clause applied on Column4 Output getting is like

Column1 Column2 Column3 Column5(Summation of Column3)
1 20140221 0 0
1 20140222 10 10
1 20140225 20 30(20+10)
1 20140228 30 60(30+30)(As Column4 is having value B for this date so due to group by clause it doesn't add the data from previous row.)

Please let me know some alternative of GROUP by clause or other alternative. Your quick help will be much appreciated.

Thanks, Nupur

more ▼

asked Nov 13, 2017 at 04:40 PM in Default

avatar image


what query are you using?

Nov 13, 2017 at 04:48 PM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Does this not have an answer to your question? Calculating-simple-running-totals-in-SQL-Server

more ▼

answered Nov 14, 2017 at 08:42 PM

avatar image

31 3

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Nov 13, 2017 at 04:40 PM

Seen: 20 times

Last Updated: Nov 14, 2017 at 08:42 PM

Copyright 2018 Redgate Software. Privacy Policy