Linked Questions
168 questions linked to/from Calculate a Running Total in SQL Server
0 votes
0 answers
41 views
sql running total over duplicate values [duplicate]
i want to do running total but there is no unique column or id column to be used in over clause. CREATE TABLE piv2([name] varchar(5), [no] int); INSERT INTO piv2 ([name], [no]) VALUES ('a', ...
-2 votes
1 answer
40 views
How to avoid using while/cursor to achieve the same query results? [duplicate]
I want to avoid looping from the below query.Now it takes long to retrieve the results from a table with 100000 records. @iMAX int, @rowId int, @currCode varchar(20), ...
-4 votes
1 answer
37 views
How to get below result [duplicate]
I have below given table ID Value 1 10 2 20 3 40 I want below output ID Value Total 1 10 10 2 20 30 3 40 70 I want to calculate total by adding value in ...
0 votes
0 answers
35 views
How do I make a Running Total? [duplicate]
I have a Query made that takes all invoices from a specific customer and it shows all the payment information and current balance, as shown in the image of excel Ex: Img1 The idea is to add the ...
0 votes
1 answer
33 views
I have a sql server table like as follows below [duplicate]
I have a table with two cols like as follows below ID SAL 1 1000 2 2000 3 3000 4 4000 5 5000 I'd like to have an output like that: ID SAL 1 1000 2 3000 3 6000 4 10000 5 15000 */...
0 votes
0 answers
31 views
SQL Cumulative Total Grouped [duplicate]
I am trying to create a cumulative total for the Paid column (SUMPaid), grouped by Contract and Ordered by Year. I tried using SUM with Partition By but that SUMs the group from the first line, I ...
0 votes
1 answer
21 views
SQL Server 2012 running total by date [duplicate]
I have a table that contains column [Month/Year], [Date], [Volume] When i run this query "select [Month/Year], [date], sum([Volume]) as 'sum' from VOLUME where [Month/Year] = '2018-10-01' group by [...
257 votes
16 answers
621k views
How to get cumulative sum
declare @t table ( id int, SomeNumt int ) insert into @t select 1,10 union select 2,12 union select 3,3 union select 4,15 union select 5,23 select * from @t the above select ...
130 votes
6 answers
311k views
Partition Function COUNT() OVER possible using DISTINCT
I'm trying to write the following in order to get a running total of distinct NumUsers, like so: NumUsers = COUNT(DISTINCT [UserAccountKey]) OVER (PARTITION BY [Mth]) Management studio doesn't seem ...
16 votes
2 answers
12k views
nvarchar concatenation / index / nvarchar(max) inexplicable behavior
I today ran into a really weird problem in SQL Server (both 2008R2 and 2012). I'm trying to build up a string using concatenation in combination with a select statement. I found the resulting string ...
23 votes
2 answers
3k views
Why are logical reads for windowed aggregate functions so high?
I've found that in execution plans using common subexpression spools that the reported logical reads get quite high for large tables. After some trial and error I've found a formula that seems to ...
12 votes
4 answers
32k views
SQL Cumulative Count
I have table with departments. I need to count how many people are within which dept. This is easily done by SELECT DEPT, COUNT(*) as 'Total' FROM SR GROUP BY DEPT; Now I need to also ...
15 votes
2 answers
39k views
SQL: use WHERE clause in OVER()?
How can I use WHERE clause to filter in the OVER clause? i.e. from the following data LoanID | Principal | Tenor | AmortizingPrincipal ---------------------------------------- 1 20000 ...
8 votes
1 answer
35k views
Calculating a running count & running total across customers with SQL
I have the following table (SQL Server 2012): DID - cust id GID - order id AMT - order amt Gf_Date - order date SC - order reversal amount I'm trying to calculate a running count of orders and a ...
7 votes
3 answers
4k views
subquery or leftjoin with group by which one is faster?
i have to show running total with the total column in my application ... so i have used the following queries for finding the running total... and i find that both are working as per my need . in one ...